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)
//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
方法中,即使没有更多的行,也不停止地连续删除行
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)
确保行和列为0或大于0。因为当行和列不存在时,它们可能是-1。那么您将会得到很多错误
所以这一行很重要
(row >= 0 && col >= 0){}
boolean ishotKeyColEmpty = tableModel.getValueAt(row, 0).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();
}
}
}
但仍然无法修复。我的目的是当用户在组合框中选择delete选项时,我只想完全删除该行。顺便说一下,removeRow()方法可以在侦听器外部正常工作。下面是代码和说明: 我是不是漏了什么重要的地方?如有任何帮助,我们将不胜感激。
我的Jtable有一个listSelectionListener: 我创建了一个按钮来删除带有事件的Jtable的所有行: 如果我在没有选择任何行的情况下按下按钮,则不会出现错误,但当我选择一行,然后按下按钮时,会出现以下错误: 当表没有ListSseltionListener时,不会发生这种情况。我哪里错了? 提前感谢您的帮助。
问题内容: 我想删除DefaultTable的所有行。我发现有两种常见的方法可以在Internet上删除它们,但是在我的情况下它们都不起作用,因为这些方法在DefaultTableModel中不存在。我想知道为什么:S。我使用DefaultTableModel的代码是 DefaultTableModel Table =(DefaultTableModel)Table.getModel(); 删除的
我正在用随机整数填充ArrayList。然后,当我试图删除它们时,我得到了以下错误: 线程“main”java.lang.indexoutOfboundsException:在java.base/jdk.internal.util.preconditions.outofbounds(preconditions.java:64)在java.base/jdk.internal.util.precond
我有三层根布局、主页和内容(rootlayout.fxml、home.fxml和content.fxml) 像这样,我正在添加内容布局。在rootlayout.fxml中,我有一个home按钮。我的要求是,如果一个用户点击主页按钮,那么我希望内容布局被删除,主页布局是可见的。 在我创建的Controller(在我指向同一个Controller的所有fxml文件中)类中 我面临的问题是我得到了Nul
员工与部门之间的关联是多对多的双向关联。 我正在尝试在删除受雇者之前删除关联