Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding a column of JRadioButtons to a JTable

 
guy madura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
guy madura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
guy madura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jyothi ve
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic