---
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 [#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)
* Source Code Examples [#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) {
removeAll();
initButtons();
buttons[((Answer) v).ordinal()].setSelected(true);
// 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;
// }
}
}
}
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;
}
}
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;
// ...
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、`JRadioButton`を`3`つ配置した`JPanel`を`CellRenderer`と`CellEditor`用に`2`つ用意しています。`CellEditor`内の各`JRadioButton`にはクリックされたら編集を終了して更新を`TableModel`にコミットするための`ActionListener`を追加しています。
* Reference [#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
#comment