Swing/LineFocusTable のバックアップの現在との差分(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/LineFocusTable へ行く。
- 1 (2006-06-05 (月) 12:54:07)
- 2 (2006-06-06 (火) 12:42:37)
- 3 (2006-06-17 (土) 19:45:06)
- 4 (2007-07-20 (金) 14:22:42)
- 5 (2008-07-16 (水) 18:53:19)
- 6 (2011-03-18 (金) 19:05:46)
- 7 (2011-05-24 (火) 04:01:36)
- 8 (2013-02-28 (木) 14:49:30)
- 9 (2014-11-22 (土) 03:59:58)
- 10 (2014-12-02 (火) 01:24:58)
- 11 (2015-03-11 (水) 18:27:08)
- 12 (2016-12-22 (木) 12:25:47)
- 13 (2017-12-09 (土) 02:01:05)
- 14 (2018-02-24 (土) 19:51:30)
- 15 (2018-08-16 (木) 14:18:42)
- 16 (2019-08-29 (木) 15:55:57)
- 17 (2021-04-22 (木) 21:55:53)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのフォーカスを一行全体に適用する #navi(../) *JTableのフォーカスを一行全体に適用する [#n5213a17] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-06-05~ 更新日:&lastmod; --- category: swing folder: LineFocusTable title: JTableのフォーカスを一行全体に適用する tags: [JTable, Focus, Border] author: aterai pubdate: 2006-06-05T12:54:07+09:00 description: JTableのフォーカスをセルではなく、一行全体に掛かっているように表示します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTPOarzqiI/AAAAAAAAAdo/uwFLFlU_EpI/s800/LineFocusTable.png hreflang: href: https://java-swing-tips.blogspot.com/2011/05/change-border-of-focused-row-in-jtable.html lang: en --- * 概要 [#summary] `JTable`のフォーカスをセルではなく、一行全体に掛かっているように表示します。 #contents #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTPOarzqiI/AAAAAAAAAdo/uwFLFlU_EpI/s800/LineFocusTable.png) **概要 [#b3d73591] JTableのフォーカスをセルではなく、一行全体に掛かっているように表示します。 * サンプルコード [#sourcecode] #code(link){{ enum Type { START, END } #screenshot class DotBorder extends EmptyBorder { private static final BasicStroke DASHED = new BasicStroke( 1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, new float[] {1f}, 0f); private static final Color DOT_COLOR = new Color(200, 150, 150); public final Set<Type> type = EnumSet.noneOf(Type.class); **サンプルコード [#t2e1e111] private class DotBorder extends EmptyBorder { private final BasicStroke dashed = new BasicStroke( 1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[]{1.0f}, 0.0f); public boolean isBorderOpaque() {return true;} public DotBorder(Insets borderInsets) { super(borderInsets); } public DotBorder(int top, int left, int bottom, int right) { super(top, left, bottom, right); } public void paintBorder( Component c, Graphics g, int x, int y, int w, int h) { Graphics2D g2 = (Graphics2D)g; g2.translate(x,y); if(isFocusedBorder()) { g2.setPaint(new Color(200,150,150)); g2.setStroke(dashed); int cbx = c.getBounds().x; if(cbx==0) { g2.drawLine(0,0,0,h); } if(end) { g2.drawLine(w-1,0,w-1,h); } if(cbx%2==0) { g2.drawLine(0,0,w,0); g2.drawLine(0,h-1,w,h-1); }else{ g2.drawLine(1,0,w,0); g2.drawLine(1,h-1,w,h-1); } } g2.translate(-x,-y); } } protected DotBorder(int top, int left, int bottom, int right) { super(top, left, bottom, right); } -&jnlp; -&jar; -&zip; @Override public boolean isBorderOpaque() { return true; } **解説 [#lbc490ab] レンダラーでフォーカスの有るセルをJTable#getSelectionModel()#getLeadSelectionIndex()で取得し、独自ラベルで最初と最後のセルの垂直の点線、途中のセルの水平点線を描画しています。 @Override public void paintBorder( Component c, Graphics g, int x, int y, int w, int h) { Graphics2D g2 = (Graphics2D) g.create(); g2.translate(x, y); g2.setPaint(DOT_COLOR); g2.setStroke(DASHED); if (type.contains(Type.START)) { g2.drawLine(0, 0, 0, h); } if (type.contains(Type.END)) { g2.drawLine(w - 1, 0, w - 1, h); } if (c.getBounds().x % 2 == 0) { g2.drawLine(0, 0, w, 0); g2.drawLine(0, h - 1, w, h - 1); } else { g2.drawLine(1, 0, w, 0); g2.drawLine(1, h - 1, w, h - 1); } g2.dispose(); } } }} カラム幅を変更するなどの操作を行っても、セル上下の水平点線のつなぎ目でドットが重ならないようにするため、偶数奇数ドット目のどちらで始まっているかで開始位置をずらしています。 * 解説 [#explanation] - デフォルトの`JTable`では`JTable#setRowSelectionAllowed(true)`を設定すると選択状態は一行ごとになるが、フォーカスはセル単位で描画される - 上記のサンプルではセルレンダラーの描画メソッド内で`JTable#getSelectionModel()#getLeadSelectionIndex()`を使用してフォーカスが存在するセルを取得し、独自ラベルを使って最初と最後のセルの垂直の点線、途中のセルの水平点線を追加することでフォーカスが一行全体に適用されているように見せている -- カラム幅を変更するなどの操作を行ってもセル上下の水平点線のつなぎ目でドットが重ならないようにするため、偶数奇数ドット目のどちらで始まっているかを判断して開始位置のオフセットを決定する //**参考リンク **コメント [#ld8c86d7] * 参考リンク [#reference] - [[JTableの次行にTabキーでフォーカスを移動>Swing/SelectNextRow]] * コメント [#comment] #comment #comment