I have added radio buttons to a JTable
using renderer and editor. I have also created group for the same. I'm unable to achieve the exclusiveness (only 1 radio button should be selected) using this principle. Please see my code below and appreciate your response.
Renderer and Editor classes:
class RadioButtonRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
return (Component) value;
}
}
class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
private JRadioButton button;
public RadioButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value == null)
return null;
button = (JRadioButton) value;
button.addItemListener(this);
return (Component) value;
}
public Object getCellEditorValue() {
button.removeItemListener(this);
return button;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
This is where the data is prepared and the grouping is done:
private void displayPhoneListShow(Person person) {
DefaultTableModel dm = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return true;
}
};
Object[] objects = new Object[3];
Object[] tableColumnNamesPhone = new Object[3];
tableColumnNamesPhone[0] = "Select";
tableColumnNamesPhone[1] = "Phone Number";
tableColumnNamesPhone[2] = "Preferred";
dm.setColumnIdentifiers(tableColumnNamesPhone);
ListIterator<Phone> phoneList = person.getPhoneList().listIterator();
while (phoneList.hasNext()) {
Phone phone = phoneList.next();
objects[0] = new JRadioButton(("^"));
objects[1] = phone.getPhoneNumber();
objects[2] = phone.getPreferred();
dm.addRow(objects);
}
this.phoneTable.setModel(dm);
ButtonGroup group = new ButtonGroup();
for (int row = 0; row < dm.getRowCount(); row++) {
JRadioButton radio = (JRadioButton) dm.getValueAt(row, 0);
group.add(radio);
}
phoneTable.getColumn("Select").setCellRenderer(
new RadioButtonRenderer());
phoneTable.getColumn("Select").setCellEditor(
new RadioButtonEditor(new JCheckBox()));
}
I am still unable to achieve the result. I have also tried adding ButtonGroup group = new ButtonGroup()
as a class variable and also part of the RadioButtonRenderer
class and added buttons to this group. But results were indifferent.
Please suggest.
As an alternative, use a
JComboBox
as an editor for mutually exclusive choices within a row. For convenience,DefaultCellEditor
offers a constructor especially for this. The result also makes more efficient use of horizontal space in the row.DependentColumn
is an example, shown below. See also these other alternatives.Figure from @mKorbel's
StatusRenderer
andStatusEditor
, Mac OS X:Figure from a related example, Mac OS X:
Figure from @mKorbel's
StatusRenderer
andStatusEditor
, Windows:Figure using
DefaultTableCellRenderer
andDefaultCellEditor
, Windows (see comments):Code: