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