gpt4 book ai didi

java - Swing JTable,从 DefaultTableModel 中删除行时出现异常

转载 作者:行者123 更新时间:2023-11-29 05:03:33 25 4
gpt4 key购买 nike

我编写了以下函数,我可以使用它从 DefaultTableModel 中删除行:

private static void removeTableRows(JTable tableToClear){
DefaultTableModel defaultModel =
(DefaultTableModel) tableToClear.getModel();
int rows = defaultModel.getRowCount();

while(rows>0) {
defaultModel.removeRow(0);
}
String[] test = {"a","b","c"};
defaultModel.addRow(test);
}

抛出以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at org.swx.nursing.tools.gui.DefaultTableGui$2.tableChanged(DefaultTableGui.java:217)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:245)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:447)
at org.swx.nursing.tools.gui.DefaultTableGui.removeTableRows(DefaultTableGui.java:357)
at org.swx.nursing.tools.gui.DefaultTableGui.actionPerformed(DefaultTableGui.java:330)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

tablechanged方法重写如下:

 //Add table cell listener
model.addTableModelListener(new TableModelListener(){

public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int col = e.getColumn();
TableModel tableModel = (TableModel)e.getSource();
Object data = null ;

try{
data = tableModel.getValueAt(row, col);
} catch (Exception ex) {
LOGGER.error("Failed to access table data: "+ex.getMessage());
}

boolean ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();
boolean ishTypeColEmpty = tableModel.getValueAt(row, 2).toString().isEmpty();
/*
* If the Application path column is empty, then clear the contents from
* the hotKey and Type cells for this row
*
*/
if(data != null
&& data.toString().equals("")
&& !ishotKeyColEmpty
&& !ishTypeColEmpty) {
tableModel.setValueAt("", row, 0);
tableModel.setValueAt("", row, 2);
}
}
});

我不确定是什么导致了这个异常..

最佳答案

您的代码中有 2 个问题。

1) 尝试从不存在的行/列中获取值 - 在 modellistner 中。

2)即使在 removeTableRows 方法中没有更多行的情况下,也不停止地连续删除行


让我先解释第二个错误。

2) 在 while 循环内您没有更新行数。您正在获取行数,但此 while 循环条件永远不会变为 false。例如,您有 3 行并且现在在 while 内计数为 3 loop 你正在检查 (rows > 0) 无论它循环了多少次,它总是正确的,所以当循环运行第 4 次时,你会得到一个错误,因为没有更多的行可以删除。你必须重新计算行数

将方法更改为

DefaultTableModel defaultModel= (DefaultTableModel) tableToClear.getModel();
int rows = defaultModel.getRowCount();
while (rows > 0) {
defaultModel.removeRow(0);
rows = defaultModel.getRowCount(); // this is very important
}
String[] test = {"a", "b", "c"};
defaultModel.addRow(test);


注意:

您可以使用方法 defaultModel.RowCount(0); 方法来删​​除所有行。您不需要使用循环来完成

1) 确保 row 和 col 为 0 或大于它。因为当它们不存在时它们可以为 -1。那么你会得到很多错误

所以这一行很重要

 (row >= 0 && col >= 0){}

还有这里

boolean ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();

你必须在调用之前安全地检查 getValueAt(row, 0) 是否为 null toString().isEmpty() 方法。 在你的

完整代码

addTableModelListener(new TableModelListener() {

@Override
public void tableChanged(TableModelEvent e) {

int row = e.getFirstRow();
int col = e.getColumn();
TableModel tableModel = (TableModel) e.getSource();
Object data = null;

try {
if (row >= 0 && col >= 0) {
data = tableModel.getValueAt(row, col);

boolean ishotKeyColEmpty = false;
boolean ishTypeColEmpty = false;
if (tableModel.getValueAt(row, 0) != null) {
ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();
}
if (tableModel.getValueAt(row, 2) != null) {
ishTypeColEmpty = tableModel.getValueAt(row, 2).toString().isEmpty();
}

if (data != null && data.toString().equals("") && !ishotKeyColEmpty && !ishTypeColEmpty) {
tableModel.setValueAt("", row, 0);
tableModel.setValueAt("", row, 2);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

关于java - Swing JTable,从 DefaultTableModel 中删除行时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com