Swing/CheckBoxesInTableCell のバックアップの現在との差分(No.15)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CheckBoxesInTableCell へ行く。
- 1 (2011-03-01 (火) 14:22:06)
- 2 (2011-03-01 (火) 21:12:24)
- 3 (2011-03-03 (木) 13:12:32)
- 4 (2011-03-04 (金) 15:57:16)
- 5 (2011-03-09 (水) 22:25:10)
- 6 (2011-03-10 (木) 16:15:06)
- 7 (2011-03-10 (木) 19:01:22)
- 8 (2011-03-15 (火) 16:12:51)
- 9 (2011-03-15 (火) 19:21:35)
- 10 (2011-11-01 (火) 18:12:50)
- 11 (2011-11-10 (木) 18:49:57)
- 12 (2012-05-21 (月) 14:14:17)
- 13 (2012-12-23 (日) 05:46:28)
- 14 (2013-04-06 (土) 05:23:26)
- 15 (2013-07-27 (土) 01:06:49)
- 16 (2013-08-17 (土) 01:14:38)
- 17 (2013-10-10 (木) 11:39:38)
- 18 (2014-06-26 (木) 16:17:59)
- 19 (2014-10-07 (火) 17:45:51)
- 20 (2014-11-21 (金) 18:28:54)
- 21 (2015-03-06 (金) 19:02:50)
- 22 (2015-03-25 (水) 17:01:50)
- 23 (2015-04-02 (木) 15:10:13)
- 24 (2016-01-12 (火) 17:53:22)
- 25 (2017-06-20 (火) 13:51:57)
- 26 (2017-08-07 (月) 19:48:14)
- 27 (2018-02-24 (土) 19:51:30)
- 28 (2018-08-09 (木) 18:19:41)
- 29 (2019-02-20 (水) 15:32:34)
- 30 (2019-05-22 (水) 19:34:28)
- 31 (2020-12-06 (日) 00:14:12)
- 32 (2023-03-16 (木) 15:44:30)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのCellにJCheckBoxを複数配置する #navi(../) #tags(JTable, JCheckBox, TableCellRenderer, TableCellEditor, JPanel, InputMap, ActionMap) RIGHT:Posted by &author(aterai); at 2011-02-28 *JTableのCellにJCheckBoxを複数配置する [#v80be979] ``JTable``のセル中に``JCheckBox``を複数個配置します。 --- category: swing folder: CheckBoxesInTableCell title: JTableのCellにJCheckBoxを複数配置する tags: [JTable, JCheckBox, TableCellRenderer, TableCellEditor, JPanel, InputMap, ActionMap] author: aterai pubdate: 2011-02-28T15:07:56+09:00 description: JTableのセル中にJCheckBoxを複数個配置します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TWs6JY73P8I/AAAAAAAAA2M/wwrwT7R5K4k/s800/CheckBoxesInTableCell.png hreflang: href: https://java-swing-tips.blogspot.com/2011/03/checkboxes-in-jtable-cell.html lang: en --- * 概要 [#summary] `JTable`のセル中に`JCheckBox`を複数個配置します。 -&jnlp; -&jar; -&zip; #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TWs6JY73P8I/AAAAAAAAA2M/wwrwT7R5K4k/s800/CheckBoxesInTableCell.png) //#screenshot #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TWs6JY73P8I/AAAAAAAAA2M/wwrwT7R5K4k/s800/CheckBoxesInTableCell.png) **サンプルコード [#p4dd25d7] * サンプルコード [#sourcecode] #code(link){{ class CheckBoxesPanel extends JPanel { private static final String OSNAME = System.getProperty("os.name"); protected final String[] title = {"r", "w", "x"}; public JCheckBox[] buttons; public CheckBoxesPanel() { super(); setOpaque(false); setBackground(new Color(0,0,0,0)); setBackground(new Color(0x0, true)); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); initButtons(); } protected void initButtons() { buttons = new JCheckBox[title.length]; for(int i=0; i<buttons.length; i++) { for (int i = 0; i < buttons.length; i++) { JCheckBox b = new JCheckBox(title[i]); b.setOpaque(false); b.setFocusable(false); b.setRolloverEnabled(false); b.setBackground(new Color(0,0,0,0)); b.setBackground(new Color(0x0, true)); buttons[i] = b; add(b); add(Box.createHorizontalStrut(5)); } } private static final String OSNAME = System.getProperty("os.name"); protected void updateButtons(Object v) { if("Windows 7".equals(OSNAME)) { //Windows aero? if ("Windows 7".equals(OSNAME)) { //Windows aero? removeAll(); initButtons(); } Integer i = (Integer)(v==null?0:v); buttons[0].setSelected((i&(1<<2))!=0); buttons[1].setSelected((i&(1<<1))!=0); buttons[2].setSelected((i&(1<<0))!=0); Integer i = (Integer) (v == null ? 0 : v); buttons[0].setSelected((i & (1 << 2)) != 0); buttons[1].setSelected((i & (1 << 1)) != 0); buttons[2].setSelected((i & (1 << 0)) != 0); } } }} #code{{ class CheckBoxesRenderer extends CheckBoxesPanel implements TableCellRenderer, Serializable { public CheckBoxesRenderer() { super(); setName("Table.cellRenderer"); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { updateButtons(value); return this; } public static class UIResource extends CheckBoxesRenderer implements UIResource{} public static class UIResource extends CheckBoxesRenderer implements UIResource {} } }} #code{{ class CheckBoxesEditor extends CheckBoxesPanel implements TableCellEditor, Serializable { public CheckBoxesEditor() { ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fireEditingStopped(); } }; ActionMap am = getActionMap(); for(int i=0; i<buttons.length; i++) { for (int i = 0; i < buttons.length; i++) { final JCheckBox b = buttons[i]; b.addActionListener(al); am.put(title[i], new AbstractAction(title[i]) { public void actionPerformed(ActionEvent e) { @Override public void actionPerformed(ActionEvent e) { b.setSelected(!b.isSelected()); fireEditingStopped(); } }); } InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), title[0]); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), title[1]); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0), title[2]); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { updateButtons(value); return this; } @Override public Object getCellEditorValue() { int i = 0; if(buttons[0].isSelected()) i|=1<<2; if(buttons[1].isSelected()) i|=1<<1; if(buttons[2].isSelected()) i|=1<<0; if (buttons[0].isSelected()) i |= 1 << 2; if (buttons[1].isSelected()) i |= 1 << 1; if (buttons[2].isSelected()) i |= 1 << 0; return i; } //Copid from AbstractCellEditor // Copied from AbstractCellEditor protected EventListenerList listenerList = new EventListenerList(); transient protected ChangeEvent changeEvent = null; //...... // ... }} **解説 [#mb2dc2f3] 上記のサンプルでは、``JTable``のセル内に3つの``JCheckBox``を配置した``JPanel``を作成し、これを``CellRenderer``と``CellEditor``として別々に使用しています。``JCheckBox``をマウスでクリックすると、その``JCheckBox``の選択状態だけが変化します。 * 解説 [#explanation] - `JTable`のセル内に`3`つの`JCheckBox`を配置した`JPanel`を作成 -- この`JPanel`を`CellRenderer`用と`CellEditor`用として別々に使用 - `JCheckBox`をマウスでクリックすると直下にある`JCheckBox`の選択状態のみ更新 - 編集中にカラムヘッダの移動やリサイズ(`JFrame`などのリサイズに連動)などが発生してもチェックした内容がリセットされないように`CellEditor`のチェックボックスがクリックされたら`fireEditingStopped()`メソッドを呼び出して編集を終了し更新を確定する ---- ヘッダカラムの移動、リサイズ(``JFrame``などのリサイズ)で、チェックした内容が消えてしまわないように、``CellEditor``のチェックボックスがクリックされたら``fireEditingStopped()``メソッドを呼び出して編集を終了し更新を確定するようにしています。 * 参考リンク [#reference] - [[JTableのセル中にJRadioButtonを配置>Swing/RadioButtonsInTableCell]] - [[JTableのセルに複数のJButtonを配置する>Swing/MultipleButtonsInTableCell]] - [[JCheckBoxのセルをロールオーバーする>Swing/RolloverBooleanRenderer]] %%``JTable``自体に以下の様な``MouseListener``を追加してチェックボックスがクリックされるたびに``table.getCellEditor(row, col).stopCellEditing();``を呼び出しています。%% * コメント [#comment] #comment - ビットフラグを`EnumSet`に変更したサンプルを[[JTableの列にEnumSetを使用する>Swing/EnumSet]]に移動。 -- &user(aterai); &new{2011-03-01 (火) 14:22:06}; - `rwx`セルを選択中にKBD{R}、KBD{W}、KBD{X}キーを入力すると、そのチェックが切り替わるように`InputMap`、`ActionMap`を追加。 -- &user(aterai); &new{2011-03-09 (水) 22:33:39}; - `Windows`環境で`Aero`効果を有効にしていると?、残像が表示される場合がある障害を修正。 -- &user(aterai); &new{2011-11-01 (火) 18:12:50}; //#code{{ //table.addMouseListener(new MouseAdapter() { // @Override public void mouseReleased(MouseEvent e) { // JTable t = (JTable)e.getComponent(); // Point p = e.getPoint(); // int row = t.rowAtPoint(p); // int col = t.columnAtPoint(p); // if(t.convertColumnIndexToModel(col)==1) { // t.getCellEditor(row, col).stopCellEditing(); // } // } //}); //}} //JTableにではなく、CellEditor自体にMouseListenerを追加する方法もあります。 //#code{{ //class CheckBoxEditorRenderer2 extends CheckBoxEditorRenderer // implements MouseListener { // private final JTable table; // public CheckBoxEditorRenderer2(JTable table) { // super(); // this.table = table; // editor.addMouseListener(this); // } // //Copied form http://tips4java.wordpress.com/2009/07/12/table-button-column/ // private boolean isButtonColumnEditor; // @Override public void mousePressed(MouseEvent e) { // if(table.isEditing() && table.getCellEditor() == this) { // isButtonColumnEditor = true; // } // } // @Override public void mouseReleased(MouseEvent e) { // if(isButtonColumnEditor && table.isEditing()) { // table.getCellEditor().stopCellEditing(); // } // isButtonColumnEditor = false; // } // @Override public void mouseClicked(MouseEvent e) {} // @Override public void mouseEntered(MouseEvent e) {} // @Override public void mouseExited(MouseEvent e) {} //} //}} **参考リンク [#wc856fe8] -[[JTableのセル中にJRadioButtonを配置>Swing/RadioButtonsInTableCell]] -[[JTableのセルに複数のJButtonを配置する>Swing/MultipleButtonsInTableCell]] -[[JCheckBoxのセルをロールオーバーする>Swing/RolloverBooleanRenderer]] **コメント [#xf4aaa77] - ビットフラグを``EnumSet``に変更するテスト -- [[aterai]] &new{2011-03-01 (火) 14:22:06}; -- [[JTableの列にEnumSetを使用する>Swing/EnumSet]]に移動 - rwxセル選択中にキーボードからr,w,xを入力するとチェックが切り替わるように``InputMap, ActionMap``を追加。 -- [[aterai]] &new{2011-03-09 (水) 22:33:39}; - ``Windows``環境で``Aero``効果を有効にしていると?、残像が表示される場合がある?のを修正。 -- [[aterai]] &new{2011-11-01 (火) 18:12:50}; #comment