0

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.

Deepak
  • 335
  • 1
  • 7
  • 21
  • If you don't get help soon, consider creating and posting an [sscce](http://sscce.org) where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. – Hovercraft Full Of Eels Sep 02 '12 at 19:27
  • Thanks HoverCraft for guiding... In the mean time I figured out the solution myself.. :) still open to better solution if any – Deepak Sep 02 '12 at 19:31
  • 1
    Possible duplicate of [How to add JRadioButton to group in JTable](http://stackoverflow.com/questions/11176480/how-to-add-jradiobutton-to-group-in-jtable); see also this [answer](http://stackoverflow.com/a/11259671/230513). – trashgod Sep 02 '12 at 19:48
  • Wow, and what an answer from @trashgod too! 10+ – Hovercraft Full Of Eels Sep 02 '12 at 20:05
  • @HovercraftFullOfEels: The original was due to @mKorbel, but I'll take credit for the `enum`. :-) – trashgod Sep 02 '12 at 20:15
  • no need to call repaint in a tableModelLister, all necessary updates happen automagically - provided the model isn't confused... Which here it is: your usage is completely wrong: **never-ever** change the underlying dataVector from somewhere outside the model, instead use the api of the model to do so. – kleopatra Sep 03 '12 at 08:05

1 Answers1

0

In my view My problem is that I am not finishing the CellEditing Task and when user selects another section/class jtable celleditor simply applies previous values to the new data causing problem. So I Added

if(jTable.isEditing()){
jTable.getCellEditor().stopCellEditing();
}

Please correct me if I am wrong...

Deepak
  • 335
  • 1
  • 7
  • 21