问题描述
我有一个带有自定义对象行的JTable,其中一列是倒计时的时间组件。当倒数到0时,该行将自动移除。
我也有一个通过文本框的过滤器选项,用户可以输入数据过滤行。
我的自定义表模型看起来像这样
公共对象getValueAt(int rowIndex,int columnIndex){
CustomObject myObject = customObjects.get(rowIndex);
if(columnIndex == MyFirstColumn){
return myObject.getData1();
} else if(columnIndex == MySecondColumn){
return myObject.getData2();
} else if(columnIndex == TimeRemainingColumn){
if(myObject.getDate() - System.currentTimeMillis()< = 0){
//如果我评论这我可以过滤,但不能删除行
removeRow(myObject);
返回0;
}
else {
fireTableDataChanged();
返回myObject.getDate() - System.currentTimeMillis();
$ b $ return DateFormat.getDateInstance()。format(new Date(myObject.getDate()));
$ b public void removeRow(CustomObject object){
int row = customObjects.indexOf(object);
customObjects.remove(object);
fireTableRowsDeleted(row,row);
$ / code $ / pre
这会抛出这个异常
异常在线程 AWT-EventQueue的-0 java.lang.IndexOutOfBoundsException:无效索引
。在javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:514)
。在javax.swing.JTable.convertRowIndexToModel(JTable.java:2645)
在javax.swing.JTable.getValueAt(JTable.java:2720)
在javax.swing.JTable.prepareRenderer( JTable.java:5718)
在javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
在javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:在javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365 581)
)
在javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
。在javax.swing.JComponent.paintComponent(JComponent.java:778)
在javax.swing.JComponent.paint(JComponent.java:1054)
在javax.swing.JComponent.paintToOffscreen(JComponent中.java:5221)
....
G (DefaultToRowSorter.java)是这样的例外:
$ public $ convertRowIndexToModel(int index){
if(viewToModel = = null){
if(index< 0 || index> = getModelWrapper()。getRowCount()){
throw new IndexOutOfBoundsException(Invalid index);
}
回报指数;
}
return viewToModel [index] .modelIndex;
另一个级别,为什么删除工作没有一个分拣机(sorter == null)
public int convertRowIndexToModel(int viewRowIndex){
RowSorter sorter = getRowSorter();
if(sorter!= null){
return sorter.convertRowIndexToModel(viewRowIndex);
}
return viewRowIndex;
$ / code>
调试firstRow和endRow在这一点都是0(它们应该是?)但是它似乎试图排序/过滤一个空的列表
继承我的表代码
myModel = new CustomObjectDataModel();
myTable.setModel(myModel);
mySorter = new TableRowSorter(myModel);
myTable.setRowSorter(mySorter);
myTable.setAutoCreateRowSorter(true);
我搜索了四处,发现了死链接,提示这是Java中的一个错误,确认。
我使用Swing Timers实现了这个功能,并修复了这个问题,但是这里的问题是修改表模型美国东部时间之外。
I have a JTable with rows of custom objects, one column of which is a time component that counts down. When the countdown reaches 0 the row is to remove itself automatically.
I also have a filter option via textbox that the user can type into to filter the rows by their data.
Without the sorter applied to the JTable, everything works correctly (rows removing themselves when the time counts to 0) besides filtering. Applying the sorter gives me an "java.lang.IndexOutOfBoundsException: Invalid index"
My custom table model looks like this
public Object getValueAt(int rowIndex, int columnIndex) {
CustomObject myObject = customObjects.get(rowIndex);
if (columnIndex == MyFirstColumn) {
return myObject.getData1();
} else if (columnIndex == MySecondColumn) {
return myObject.getData2();
} else if (columnIndex == TimeRemainingColumn) {
if (myObject.getDate() - System.currentTimeMillis() <= 0) {
//If I comment this out I can filter but not remove rows
removeRow(myObject);
return 0;
}
else{
fireTableDataChanged();
return myObject.getDate() - System.currentTimeMillis();
}
}
return DateFormat.getDateInstance().format(new Date(myObject.getDate()));
}
public void removeRow(CustomObject object) {
int row = customObjects.indexOf(object);
customObjects.remove(object);
fireTableRowsDeleted(row, row);
}
This throws this exception
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:514)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2645)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
....
Going to DefaultRowSorter.java it is hitting this exception
public int convertRowIndexToModel(int index) {
if (viewToModel == null) {
if (index < 0 || index >= getModelWrapper().getRowCount()) {
throw new IndexOutOfBoundsException("Invalid index");
}
return index;
}
return viewToModel[index].modelIndex;
}
Up Another level, why removing works without a sorter (sorter == null)
public int convertRowIndexToModel(int viewRowIndex) {
RowSorter sorter = getRowSorter();
if (sorter != null) {
return sorter.convertRowIndexToModel(viewRowIndex);
}
return viewRowIndex;
}
Debugging a bit firstRow and endRow are both 0 at that point (they should be?) But it seems to be attempting to sort/filter an empty list
Heres my table code
myModel = new CustomObjectDataModel();
myTable.setModel(myModel);
mySorter = new TableRowSorter(myModel);
myTable.setRowSorter(mySorter);
myTable.setAutoCreateRowSorter(true);
I've searched around and found dead links to suggesting this is a bug in Java but can't confirm.
I implemented this using Swing Timers instead as suggested and fixed the issue, but the problem here was modifying the table model outside of the EDT.
这篇关于应用分拣机时删除JTable行导致IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!