Swing/RadioButtonsInTableCell のバックアップソース(No.18)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/RadioButtonsInTableCell へ行く。
- 1 (2009-12-21 (月) 00:36:55)
- 2 (2011-03-04 (金) 15:56:17)
- 3 (2011-03-09 (水) 22:31:16)
- 4 (2011-03-11 (金) 02:40:50)
- 5 (2011-04-25 (月) 17:32:41)
- 6 (2011-11-01 (火) 18:27:59)
- 7 (2013-01-03 (木) 14:11:37)
- 8 (2013-08-17 (土) 01:15:19)
- 9 (2014-06-26 (木) 16:17:02)
- 10 (2014-09-30 (火) 00:55:22)
- 11 (2015-01-21 (水) 18:34:11)
- 12 (2015-10-14 (水) 15:42:09)
- 13 (2016-04-04 (月) 13:41:00)
- 14 (2017-04-13 (木) 14:48:39)
- 15 (2017-11-02 (木) 15:32:16)
- 16 (2018-03-28 (水) 18:19:23)
- 17 (2019-05-22 (水) 19:34:28)
- 18 (2020-03-27 (金) 14:42:37)
- 19 (2021-10-01 (金) 09:57:35)
--- category: swing folder: RadioButtonsInTableCell title: JTableのセル中にJRadioButtonを配置 tags: [JTable, JRadioButton, TableCellRenderer, TableCellEditor, JPanel, ActionListener] author: aterai pubdate: 2009-12-21T00:36:55+09:00 description: JTableのセル中に複数のJRadioButtonを配置します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png --- * 概要 [#summary] `JTable`のセル中に複数の`JRadioButton`を配置します。%%[http://www2.gol.com/users/tame/swing/examples/JTableExamples2.html JTableExamples2]%%を元に修正を行っています。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png) * サンプルコード [#sourcecode] #code(link){{ class RadioButtonsPanel extends JPanel { private final String[] answer = { Answer.A.toString(), Answer.B.toString(), Answer.C.toString()}; public JRadioButton[] buttons; public ButtonGroup bg = new ButtonGroup(); public RadioButtonsPanel() { super(); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); initButtons(); } protected void initButtons() { bg = new ButtonGroup(); buttons = new JRadioButton[answer.length]; for (int i = 0; i < buttons.length; i++) { buttons[i] = new JRadioButton(answer[i]); buttons[i].setActionCommand(answer[i]); buttons[i].setFocusable(false); buttons[i].setRolloverEnabled(false); add(buttons[i]); bg.add(buttons[i]); } } protected void updateSelectedButton(Object v) { if (v instanceof Answer) { //if ("Windows 7".equals(OSNAME)) { //Windows aero? removeAll(); initButtons(); //} switch ((Answer) v) { case A: buttons[0].setSelected(true); break; case B: buttons[1].setSelected(true); break; case C: buttons[2].setSelected(true); break; default: break; } } } } }} #code{{ class RadioButtonsRenderer extends RadioButtonsPanel implements TableCellRenderer { public RadioButtonsRenderer() { super(); setName("Table.cellRenderer"); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { updateSelectedButton(value); return this; } } }} #code{{ class RadioButtonsEditor extends RadioButtonsPanel implements TableCellEditor { public RadioButtonsEditor() { super(); ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fireEditingStopped(); } }; for (AbstractButton b: buttons) { b.addActionListener(al); } } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { updateSelectedButton(value); return this; } @Override public Object getCellEditorValue() { //bug: return bg.getSelection().getActionCommand(); return Answer.valueOf(bg.getSelection().getActionCommand()); } //Copied from AbstractCellEditor //protected EventListenerList listenerList = new EventListenerList(); transient protected ChangeEvent changeEvent = null; // ... }} * 解説 [#explanation] 上記のサンプルでは、`JRadioButton`を`3`つ配置した`JPanel`を、`CellRenderer`、`CellEditor`用に`2`つ用意しています。`CellEditor`内の各`JRadioButton`には、クリックされたら編集を終了して更新を`TableModel`にコミットするための`ActionListener`を追加しています。 * 参考リンク [#reference] - [https://tips4java.wordpress.com/2009/07/12/table-button-column/ Table Button Column ≪ Java Tips Weblog] - [[JTableのセルに複数のJButtonを配置する>Swing/MultipleButtonsInTableCell]] - [[JTableのCellにJCheckBoxを複数配置する>Swing/CheckBoxesInTableCell]] - [[JTableのセルにJRadioButton>Swing/RadioButtonCellEditor]] * コメント [#comment] #comment #comment