aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes unable to insert JRadio in JTable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "unable to insert JRadio in JTable." Watch "unable to insert JRadio in JTable." New topic
Author

unable to insert JRadio in JTable.

Rajapragada Dharani
Greenhorn

Joined: Oct 23, 2003
Posts: 15
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.

I have pasted my code below.


//This is My main class

JPanel panelList = new JPanel(new BorderLayout());

//Get the list of all Devices where each device has properties like device_name,device_number etc.

ArrayList allDevices = getMyDevices();

JRadioButton jRadio[] = new JRadioButton[allDevices.size()];
ButtonGroup radioButtonGroup = new ButtonGroup();

Vector row = new Vector(alDevices.size());
Vector addRow;

for (int i = 0; i < allDevices.size(); i++) {

Integer index = new Integer(i+1);
myDevice = (Device)allDevices.get(i);
addRow = new Vector();

addRow.add((Integer)index);
addRow.add((JRadioButton)jRadio[i]);
addRow.add(myDevice.getName());
addRow.add(myDevice.getNumber());

row.add(i,addRow);

}

MyTableModel deviceTableModel = new MyTableModel(row);
JTable deviceTable = new JTable(deviceTableModel);

//Adding all the radio buttons to the button group.
for(int x = 0;x<row.size();x++)
radioButtonGroup.add((JRadioButton)deviceTable.getValueAt(x,1));

deviceTable.getColumn("Select").setCellRenderer(new MyTableRadioButtonRenderer());
deviceTable.getColumn("Select").setCellEditor(new MyTableRadioButtonEditor(new JCheckBox()));
deviceTable.setVisible(true);
deviceTable.setIntercellSpacing(new Dimension(2,2));

panelList.add(deviceTable,BorderLayout.CENTER);


****************************************************************************


public class MyTableModel extends AbstractTableModel {


private Vector datalist;
private String[] columns = {" ", "Select", "Device Name", "Device Number"};

public MyTableModel() {
}

public MyTableModel(Vector l) {
datalist = l;
}

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 class MyTableRadioButtonRenderer implements TableCellRenderer {

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {

if (value==null) return null;

return (Component)value;
}
}


****************************************************************************


public class MyTableRadioButtonEditor extends DefaultCellEditor implements ItemListener {
private JRadioButton button;

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
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 ]
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Originally posted by Rajapragada Dharani:

****************************************************************************


public class MyTableRadioButtonRenderer implements TableCellRenderer {

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {

if (value==null) return null;

return (Component)value;
}
}


****************************************************************************

}



Hi there

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.

eg
JRadioButton rb = new JRadioButton();
if(value) rb.setSelected(true);
return rb;

Try that,
Rachel
Rajapragada Dharani
Greenhorn

Joined: Oct 23, 2003
Posts: 15
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
Put the jtable on a jscrollpane to get the headers to be visible:

myPanel.add(new JScrollPane(myTable));
Rajapragada Dharani
Greenhorn

Joined: Oct 23, 2003
Posts: 15
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
Rajapragada Dharani
Greenhorn

Joined: Oct 23, 2003
Posts: 15
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.

Regards Raja.
sammaiah kyatham
Ranch Hand

Joined: Aug 03, 2003
Posts: 104
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: unable to insert JRadio in JTable.