I need to add a column to JTable ( column of JRadioButtons).I had added through a customise renderer which implements default renderer.But the problem is the other columns works fine by having alternative color for each row except radio button column.Moreover the listener is not getting fired for radio button and also I have used customise editor which implements default cell editor.Can any one clear my doubt. Thanks in advance. Arun
To have the alternative color in your new column, you must specify in your Radio Button CellRenderer that the background color needs to be whatever it needs to be.
Then for the mouse listener, have you added a mouse listener to each cell in that column in the renderer?
Can you post the code for your Renderer and Editor.
The color change happens for all the rows and columns except the radiobutton column.I have added the code to editor and renderer.Moreover In editor getTableCellEditorComponent doesnt got executed.what should I do. Any suggestions
Sometimes you have to set the component's background to opaque(true) that the renderer returns. But I can't really make any good guesses without the code.
class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
public RadioButtonEditor(JCheckBox checkBox) { super(checkBox); ButtonGroup group = new ButtonGroup(); for (int row = 0;row < model.getRowCount();row++){ JRadioButton radio = (JRadioButton)model.getValueAt(row,0); group.add(radio); }
} 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;
if (row%2 == 0) { setBackground(Color.white); } else { setBackground(new Color(220,220,220)); } setForeground(Color.black); if (isSelected) {
//selectedBorder is a solid border in the color //table.getSelectionBackground(). setBackground(FXAppletUtil.LIGHT_BACKGROUND); }
return (Component)value;
}
public Object getCellEditorValue() { button.removeItemListener(this); return button; }
public void itemStateChanged(ItemEvent e) { super.fireEditingStopped();
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;