要仅从 JTable 的视图中隐藏一列,我正在使用该removeColumn()
方法。但它抛出异常
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:648)
我认为,从视图中删除列后,如果我修改了模型,则会弹出此异常。是因为模型正在更新表时视图中没有列吗?
在 JTable 中隐藏列的最佳方法是什么?而不是将大小设置为 0
编辑:异常不会定期发生。这是一个随机例外。无论如何这里是代码:
@Override
protected Object doInBackground() throws Exception {
........
resultDTO=//get data from database
tableModel.setDataVector(resultDTO.getAllRows(), tableModel.getColumnNames());
// hide column
table.removeColumn(table.getColumnModel().getColumn(7));
System.out.println("table column count : " + table.getColumnCount());
System.out.println("model column count : " + tableModel.getColumnCount());
........
.........
}
initial result (with out any data in table, at application startup):
table column count : 7
model column count : 8
after populating data (first running of above method):
table column count : 7
model column count : 8
after few times executing :
table column count : 7
model column count : 8
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:648)
at javax.swing.plaf.synth.SynthTableHeaderUI.paint(SynthTableHeaderUI.java:173)
有时当我第一次加载数据时会发生上述异常,有时则不会。