将RadioButton组添加到JTable

时间:2012-09-02 18:26:12

标签: java swing jtable tablecelleditor jradiobutton

我正在设计一个考勤系统,为此我使用了一个JTable来列出学生并标记出席他们的名字。 我在Jtable列中添加了一个RadioButton组,我的代码如下: -

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

上面的代码正在运行,我得到一组radiobuttons作为第4列。

我正在动态地向表中添加行,将默认选择的RadioButton设置为“4”。

 jTableDT.getDataVector().removeAllElements();
  while(rs.next())
 {

  jTableDT.insertRow(i,new Object[ {rs.getString(1),rs.getString(2),rs.getString(4),"4"});
            i++;
  }

我的问题: -

假设用户选择一个班级/部分来标记学生出勤率,并使用上述RadioButtons面板动态生成学生列表(假设表中有10行)。 现在,当用户选择另一个只有3行学生的课程/部分时,会抛出异常: -

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).........

据我所知,表以某种方式保留了radiobuttons值并将其应用于新jtable(当用户选择另一个section / class时),而不管所有行都已被删除并且已输入新行。 当选定的类/节具有比先前选择的类/节表更少的行时,也不会抛出上述异常。

有没有办法在每个类/节调用中重置tablecellrenderer或tablecelleditor(因为我正在重置tablemodel数据)?

请帮帮我。

1 个答案:

答案 0 :(得分:0)

在我看来我的问题是我没有完成CellEditing任务,当用户选择另一个部分/类jtable celleditor时,只需将先前的值应用于导致问题的新数据。 所以我添加了

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

如果我错了,请纠正我......