Forums Register Login

Adding a column of JRadioButtons to a JTable

+Pie Number of slices to send: Send
Hi everybody,

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
+Pie Number of slices to send: Send
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.

Cheers,
Rachel
+Pie Number of slices to send: Send
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

Arun
+Pie Number of slices to send: Send
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.

Cheers,
rachel
+Pie Number of slices to send: Send
Here is the code for editor

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();

}
}

Any suggestions
+Pie Number of slices to send: Send
Hi there

First - lets work on getting the color right and then we'll work on getting the events to fire for you.

In the getTableCellEditorComponent() before you return the component, call setOpaque(true) on it. This should make the background's color show up.

Then, about the listener, why are you using ItemListener?

Try and let me know, Cheers,
Rachel
+Pie Number of slices to send: Send
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 ]
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 10600 times.
Similar Threads
unable to insert JRadio in JTable.
JTable ........
Need help with RadioButtons in JTable
jtable
JTable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:02:46.