I am designing an attendance system and for this I am using a JTable For listing the students and marking the attendance against their names. I added a RadioButton Group in a Jtable column, My code of this is as follows:-
JTable jTable = new JTable(){
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
repaint();
}
};
jTableDT=new DefaultTableModel(
new Object [][] {},
new String [] {
"Enroll","Student Id", "Student Name","Attendance" });
jTable.setModel(jTableDT);
jTable.getColumn("Attendance").setCellRenderer(new RadioButtonRenderer());
jTable.getColumn("Attendance").setCellEditor(new RadioButtonEditor(new JCheckBox()));
class RadioButtonRenderer implements TableCellRenderer {
public JPanel pnl = new JPanel();
public ButtonGroup group1 = new ButtonGroup();
public JRadioButton btnOne = new JRadioButton("Present");
public JRadioButton btnTwo = new JRadioButton("Absent");
public JRadioButton btnThree = new JRadioButton("on Leave");
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
if (value==null) return null;
group1.add(btnOne );
group1.add(btnTwo );
group1.add(btnThree );
pnl.add(btnOne );
pnl.add(btnTwo );
pnl.add(btnThree );
btnThree .setSelected(false);
btnOne .setSelected(false);
btnTwo .setSelected(false);
switch(Integer.parseInt((String)value))
{
case 3:
btnThree.setSelected(true);
break;
case 2:
btnTwo.setSelected(true);
break;
case 1:
btnOne.setSelected(true);
break;
}
return pnl;
}
}
class RadioButtonEditor extends DefaultCellEditor implements ItemListener
{
public JPanel pnl = new JPanel();
public ButtonGroup group1 = new ButtonGroup();
public JRadioButton btnOne = new JRadioButton("Present");
public JRadioButton btnTwo = new JRadioButton("Absent");
public JRadioButton btnThree = new JRadioButton("On Leave");
public RadioButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {
if (value==null) return null;
group1.add(btnOne );
group1.add(btnTwo );
group1.add(btnThree );
pnl.add(btnOne );
pnl.add(btnTwo );
pnl.add(btnThree );
btnThree .setSelected(false);
btnTwo .setSelected(false);
btnOne .setSelected(false);
switch(Integer.parseInt((String)value)) {
case 3:
btnThree.setSelected(true);
break;
case 2:
btnTwo.setSelected(true);
break;
case 1:
btnOne.setSelected(true);
break;
}
return pnl;
}
public Object getCellEditorValue() {
if(btnTwo.isSelected() == true)
return "2";
if(btnOne.isSelected() == true)
return "1";
if(btnThree.isSelected() == true)
return "3";
return "";
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
The above Code is working and I am getting a set of radiobuttons as 4th column.
I am dynamically adding rows to the table, Having the default selected RadioButton as "4".
jTableDT.getDataVector().removeAllElements();
while(rs.next())
{
jTableDT.insertRow(i,new Object[ {rs.getString(1),rs.getString(2),rs.getString(4),"4"});
i++;
}
My Problem:-
Suppose User selects a class/section to mark the student attendance and the list of students (lets say 10 rows in the table) gets generated dynamically with the above RadioButtons panel. Now when User selects another class/section that has only 3 rows of students following exception is thrown:-
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 1
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:665)
at javax.swing.JTable.setValueAt(JTable.java:2744)
at javax.swing.JTable.editingStopped(JTable.java:4726)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141)
at javax.swing.AbstractCellEditor.stopCellEditing(AbstractCellEditor.java:85)
at javax.swing.JTable.columnMarginChanged(JTable.java:4595).........
As far as I understood that somehow table is retaining the radiobuttons values and applying it to new jtable(when user selects another section/class) irrespective of the fact that all the rows have been removed and new rows have been entered. Also above mentioned exceptions are not thrown when the selected class/section has lesser rows then its previous selected class/section table.
Is there any way of resetting the tablecellrenderer or tablecelleditor (as I am resetting the tablemodel data )on each class/section call?
Please help me.