Swing/CheckedRowColor のバックアップの現在との差分(No.12)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CheckedRowColor へ行く。
- 1 (2008-07-14 (月) 14:18:45)
- 2 (2009-06-04 (木) 20:11:24)
- 3 (2009-12-14 (月) 23:22:08)
- 4 (2009-12-16 (水) 21:25:57)
- 5 (2010-07-26 (月) 14:08:14)
- 6 (2011-03-08 (火) 18:55:35)
- 7 (2011-03-13 (日) 22:02:33)
- 8 (2012-11-21 (水) 21:38:43)
- 9 (2012-11-23 (金) 04:40:08)
- 10 (2013-01-19 (土) 20:21:46)
- 11 (2013-07-31 (水) 23:37:33)
- 12 (2013-09-08 (日) 02:34:20)
- 13 (2013-10-04 (金) 15:37:24)
- 14 (2014-11-01 (土) 00:46:09)
- 15 (2014-11-16 (日) 04:44:35)
- 16 (2014-11-25 (火) 03:03:31)
- 17 (2015-12-12 (土) 02:22:22)
- 18 (2016-06-24 (金) 16:29:25)
- 19 (2017-09-21 (木) 04:43:17)
- 20 (2017-11-02 (木) 15:34:40)
- 21 (2019-02-28 (木) 18:34:40)
- 22 (2020-12-15 (火) 10:31:16)
- 23 (2022-08-20 (土) 22:15:25)
- 24 (2023-05-19 (金) 17:12:42)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのセルにJCheckBoxを表示して行背景色を変更 #navi(../) #tags(JTable, JCheckBox, TableModelListener, TableCellRenderer, TableCellEditor) RIGHT:Posted by &author(aterai); at 2008-07-14 * JTableのセルにJCheckBoxを表示して行背景色を変更 [#b7a6c77e] ``JTable``のセルに表示されている``JCheckBox``がチェックされていれば、その行の背景色を変更します。 --- category: swing folder: CheckedRowColor title: JTableのセルにJCheckBoxを表示して行背景色を変更 tags: [JTable, JCheckBox, TableModelListener, TableCellRenderer, TableCellEditor] author: aterai pubdate: 2008-07-14T14:18:45+09:00 description: JTableのセルに表示されているJCheckBoxがチェックされていれば、その行の背景色を変更します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTI7wkSMbI/AAAAAAAAATk/uGVLWCqLJUI/s800/CheckedRowColor.png --- * 概要 [#summary] `JTable`のセルに表示されている`JCheckBox`がチェックされていれば、その行の背景色を変更します。 - &jnlp; - &jar; - &zip; #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTI7wkSMbI/AAAAAAAAATk/uGVLWCqLJUI/s800/CheckedRowColor.png) #ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTI7wkSMbI/AAAAAAAAATk/uGVLWCqLJUI/s800/CheckedRowColor.png) ** サンプルコード [#h4150100] * サンプルコード [#sourcecode] #code(link){{ JTable table = new JTable(model) { @Override public Component prepareEditor( TableCellEditor editor, int row, int column) { Component cmp = super.prepareEditor(editor, row, column); if(convertColumnIndexToModel(column) == BOOLEAN_COLUMN) { JCheckBox c = (JCheckBox)cmp; c.setBackground(c.isSelected()? Color.ORANGE:getBackground()); if (convertColumnIndexToModel(column) == BOOLEAN_COLUMN) { JCheckBox c = (JCheckBox) cmp; c.setBackground(c.isSelected() ? Color.ORANGE : getBackground()); } return cmp; } @Override public Component prepareRenderer( TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); Boolean isChecked = (Boolean) model.getValueAt( convertRowIndexToModel(row), BOOLEAN_COLUMN); c.setForeground(getForeground()); c.setBackground(isChecked? Color.ORANGE:getBackground()); c.setBackground(isChecked ? Color.ORANGE : getBackground()); return c; } }; }} ** 解説 [#hd518363] 上記のサンプルでは、``JTable``の``prepareEditor``、``prepareRenderer``をオーバーライドしてセルエディタや行の背景色を変更しています。 * 解説 [#explanation] 上記のサンプルでは、`JTable`の`prepareEditor`、`prepareRenderer`をオーバーライドして編集中のセルエディタ(`JCheckBox`)や同じ行の背景色を変更しています。 ---- ``TableModelListener``でモデルが更新されると、``table.repaint()``で全体を再描画するのではなく、更新の対象になっている行だけを、以下のように再描画しています。 - デフォルトのセルレンダラーの場合、例えばチェックボックスがクリックされてモデルが更新されるとその対応するセルのみ再描画さる - このサンプルでは対象セルだけではなくそのセルが存在する行全体の背景色を変更しているので、`TableModelListener`でモデルの更新を検出し、以下のように`JTable#repaint(...)`メソッドを使って行を再描画 #code{{ model.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { if(e.getType()==TableModelEvent.UPDATE) { //rowRepaint(table, table.convertRowIndexToView(e.getFirstRow())); Rectangle r = table.getCellRect(table.convertRowIndexToView(e.getFirstRow()), 0, true); if (e.getType() == TableModelEvent.UPDATE) { // rowRepaint(table, table.convertRowIndexToView(e.getFirstRow())); Rectangle r = table.getCellRect( table.convertRowIndexToView(e.getFirstRow()), 0, true); r.width = table.getWidth(); table.repaint(r); //すこし無駄?: table.repaint(); // table.repaint(); // table全体をリペイントする必要はないので table.repaint(r); // 一行だけリペイント } } }); }} //---- //以下のような、TableCellRendererを利用する方法もあります。 //#code{{ //static class RowColorTableRenderer implements TableCellRenderer { // private final Color evenColor = new Color(240, 240, 255); // private final TableCellRenderer renderer; // public RowColorTableRenderer(TableCellRenderer renderer) { // this.renderer = renderer; // } // @Override public Component getTableCellRendererComponent( // JTable table, Object value, // boolean isSelected, boolean hasFocus, int row, int column) { // Component c = renderer.getTableCellRendererComponent(table, value, // isSelected, hasFocus, row, column); // TableModel model = table.getModel(); // Boolean isChecked = (Boolean) model.getValueAt( // table.convertRowIndexToModel(row), BOOLEAN_COLUMN); // c.setForeground(table.getForeground()); // c.setBackground(isChecked?Color.ORANGE:table.getBackground()); // return c; // } //} //}} * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#prepareRenderer-javax.swing.table.TableCellRenderer-int-int- JTable#prepareRenderer(...) (Java Platform SE 8)] - [https://community.oracle.com/thread/1361072 Swing - JTable-Row color issue] ** 参考リンク [#z0909a00] - [https://forums.oracle.com/message/5745962 JTable-Row color issue | Oracle Forums] * コメント [#comment] #comment - ごちそうさまでした。 -- &user(tanuchan); &new{2009-12-14 (Mon) 04:22:08}; -- まいどありです。 -- &user(aterai); &new{2009-12-16 (水) 21:25:57}; - メモ: [https://bugs.openjdk.org/browse/JDK-6711682 Bug ID: 6711682 JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click] -- &user(aterai); &new{2010-07-26 (月) 14:18:03}; ** コメント [#u0d463cd] - ごちそうさまでした。 -- [[tanuchan]] &new{2009-12-14 (Mon) 04:22:08}; -- まいどありです。 -- [[aterai]] &new{2009-12-16 (水) 21:25:57}; - メモ: [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6711682 Bug ID: 6711682 JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click] -- [[aterai]] &new{2010-07-26 (月) 14:18:03}; #comment