Hi All, I'am very new to swing development and working on JTable which should contain 4 columns (Index,RadioButton,Device Name and Device Number).The rows are added dynamically.I have written classes MyTableModel, MyTableRadioButtonRenderer and MyTableRadioButtonEditor which I'am using in my main class. The problem is that when I execute the main class I get a Table view with all other values as desired but the values returned by the radiobutton in the second column is null (i.e my second column is filled with empty spaces).I have tried several ways but failed in displaying the table with radiobuttons. Can anyone please guide me through this.
public String getColumnName(int col) { return columns[col]; }
public int getColumnCount() { return columns.length; }
public int getRowCount() { return datalist.size(); }
public Object getValueAt(int row, int col) {
Vector rowList = (Vector)datalist.get(row);
if(rowList.firstElement()!=null) {
switch (col) { case 0: return (Integer) rowList.elementAt(0); case 1: return (JRadioButton) rowList.elementAt(1); case 2: return (String) rowList.elementAt(2); case 3: return (String) rowList.elementAt(3); default: return null; } }else return null; }
//This one method decides exactly which cells are editable. public boolean isCellEditable(int row, int col) { switch (col) { case 0: //Index return false; case 1: //RadioButton return true; case 2: //Device Name return false; case 3: //Device Number return false; default: return false; } }
/** This one method tells the view which type of object will be displayed. * This allows the JTable to display the data in a way that is most appropriate * for the type of object that exists in that row. */ public Class getColumnClass(int col) { switch (col) { case 0: //Index return Integer.class; case 1: //RadioButton return JRadioButton.class; case 2: //Device Name return String.class; case 3: //Device Number return String.class; default: return null; } } }
public MyTableRadioButtonEditor(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(); } }
Rajapragada Dharani
Greenhorn
Joined: Oct 23, 2003
Posts: 15
posted
0
Can some one please guide me through the process of getting my view, I believe my code is confusing or not properly written.Sorry for the trouble.Thanks in advance.
[ December 05, 2004: Message edited by: Rajapragada Dharani ]
You renderer isn't working because you are cimply doing this return (Component)value
Whatever you return there is what will be shown in the column. If you want a JRadioButton, you need to make a JRadioButton that has the value stored in 'value' and return the JRadioButton.
Hi Rachel, Thanks for your reply.I understood fault in my code. I have tried some thing and now able to generate view of radio buttons. Now i made sure that null is not passes to MyTableRadioButtonRenderer class.
I have replaced some code in my main class with this in the for loop:-
for (int i = 0; i < allDevices.size(); i++) { String radioValue = "JRadioButton " + index; JRadioButton jRadio = new JRadioButton(radioValue); addRow.add((Integer)index); addRow.add(jRadio); radioButtonGroup.add(jRadio); _____________________________________________________
But earlier i forgot to mention that I was also unable to get the column headings visible.My table only shows the data in other rows.Can you please throw some light in this.
Sebastiaan Kortleven
Ranch Hand
Joined: Mar 12, 2004
Posts: 81
posted
0
Put the jtable on a jscrollpane to get the headers to be visible:
Thanks Kortleven for the reply.Your tip worked. Now I have one more query , what should i do if i want to make only one radio button set to true on a single click.Presently i'am able to select more than one at a time.Should i manully write code to make the previous selections false?
I actually added all the radio buttons to button group, this should take care of only one button been selected right.But some how this behaviour is missing.Can any one please through light on this. [ December 06, 2004: Message edited by: Rajapragada Dharani ]
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
posted
0
Rajapragada Dharani
Greenhorn
Joined: Oct 23, 2003
Posts: 15
posted
0
Thanks Craig, I understand what you said.I'am working on it, will inform as soon as there's result.Think it should work.Thanks once again.