posted 19 years ago
Please find below code to add Radio buttons as one of the JTable's column
jTable1.getColumn("Primary Role").setCellRenderer(new RadioButtonRenderer()); - Registering renderer to the column
jTable1.getColumn("Primary Role").setCellEditor(new RadioButtonEditor(new JCheckBox()) - Registering editor to the column
Below code for Renderer and Editor
class RadioButtonRenderer implements TableCellRenderer {
public JRadioButton btn = new JRadioButton();
public Component getTableCellRendererComponent(JTable table, Object
value,boolean isSelected, boolean hasFocus, int row, int column) {
if (value==null) return null;
if(((Boolean)value).booleanValue())
btn.setSelected(true);
else
btn.setSelected(false);
if (isSelected) {
btn.setForeground(table.getSelectionForeground());
btn.setBackground(table.getSelectionBackground());
} else {
btn.setForeground(table.getForeground());
btn.setBackground(table.getBackground());
}
return btn;
}
}
class RadioButtonEditor extends DefaultCellEditor
implements ItemListener {
public JRadioButton btn = new JRadioButton();
public RadioButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object
value, boolean isSelected, int row, int column) {
if (value==null) return null;
btn.addItemListener(this);
if ( ( (Boolean) value).booleanValue())
btn.setSelected(true);
else
btn.setSelected(false);
return btn;
}
public Object getCellEditorValue() {
if(btn.isSelected() == true)
return new Boolean(true);
else
return new Boolean(false);
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
[ November 05, 2004: Message edited by: jyothi ve ]
[ November 05, 2004: Message edited by: jyothi ve ]
Jyothi<br /> <br />Sun Certified Business Component Developer<br />Sun Certified Web Component Developer<br />Sun Certified Java Programmer<br />Oracle Certified SQL & PL-SQL Programmer