我正在设计一个考勤系统,为此我使用 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();
}
}
上面的代码正在运行,我将一组单选按钮作为第 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++;
}
我的问题:-
假设用户选择一个班级/部分来标记学生出勤率,并且学生列表(比如说表中的 10 行)通过上面的 RadioButtons 面板动态生成。现在,当用户选择另一个只有 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).........
据我了解,无论是否删除了所有行并输入了新行,表格都会以某种方式保留单选按钮值并将其应用于新的 jtable(当用户选择另一个部分/类时)。当所选类/部分的行数少于其先前选择的类/部分表时,也不会引发上述异常。
有没有办法在每个类/部分调用上重置 tablecellrenderer 或 tablecelleditor (因为我正在重置 tablemodel 数据)?
请帮我。