Swing/RadioButtonsInTableCell のバックアップソース(No.6)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
TITLE:JTableのセル中にJRadioButtonを配置 #navi(../) RIGHT:Posted by [[aterai]] at 2009-12-21 *JTableのセル中にJRadioButtonを配置 [#xe89889f] JTableのセル中に複数のJRadioButtonを配置します。[http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html JTableExamples2]を元に修正を行っています。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png) **サンプルコード [#f5687e69] #code{{ class RadioButtonsPanel extends JPanel { private final String[] answer = { "A", "B", "C" }; public final JRadioButton[] buttons; public final ButtonGroup bg = new ButtonGroup(); public RadioButtonsPanel() { super(); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 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("A".equals(v)) { buttons[0].setSelected(true); }else if("B".equals(v)) { buttons[1].setSelected(true); }else{ buttons[2].setSelected(true); } } } }} #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() { return bg.getSelection().getActionCommand(); } //Copid from AbstractCellEditor //protected EventListenerList listenerList = new EventListenerList(); transient protected ChangeEvent changeEvent = null; //...... }} **解説 [#rfb9e48f] 上記のサンプルでは、JRadioButtonを3つ配置したJPanelを、CellRenderer、CellEditor用に二つ用意しています。CellEditor内の各JRadioButtonには、クリックされたら編集を終了して更新をコミットするためのActionListenerを追加しています。 %%実際にどのJRadioButtonがクリックされたかなどは、以下のように、JTableに追加したMouseListenerで処理するようになっています。%% //#code{{ //table.addMouseListener(new MouseAdapter() { // @Override public void mouseReleased(MouseEvent e) { // JTable t = (JTable)e.getComponent(); // Point pt = e.getPoint(); // int row = t.rowAtPoint(pt); // int col = t.columnAtPoint(pt); // if(t.convertRowIndexToModel(row)>=0 && t.convertColumnIndexToModel(col)==1) { // TableCellEditor ce = t.getCellEditor(row, col); // //http://tips4java.wordpress.com/2009/07/12/table-button-column/ // ce.stopCellEditing(); // Component c = ce.getTableCellEditorComponent(t, null, true, row, col); // Point p = SwingUtilities.convertPoint(t, pt, c); // Component b = SwingUtilities.getDeepestComponentAt(c, p.x, p.y); // if(b instanceof JRadioButton) ((JRadioButton)b).doClick(); // } // } //}); //}} **参考リンク [#xf10c31e] -[http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html JTableExamples2] -[http://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]] **コメント [#u33af8cd] #comment