【发布时间】:2013-10-06 09:44:08
【问题描述】:
我真的需要你的帮助...我已经有点绝望了,因为我的 JTable 更新无法正常工作。
我有一个 JTable,它通过 mySQl 从数据库接收数据。我将数据存储在一个数组中并将其传递给 tableModel。触发“fireTableDatachanged”后,我看到了所有数据。这也适用于删除一行: 我只是删除数据库中的条目并从数据库中读出新数据。
所以奇怪的是:有时有效,有时无效... 我也在使用 RowSorter,这可能是实际问题。
非常感谢您的帮助,并提前致谢!
这是删除和刷新表数据的代码:
//////////////////////////////////////////////////////////////
// delete entry ButtonListener
//////////////////////////////////////////////////////////////
loeschen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// get database object
Datenbank db = new Datenbank();
// get selected row
int row = table.getSelectedRow();
int col = 0;
if (row != -1) {
row = table.convertRowIndexToModel(row);
Object entryname = model.getValueAt(row, col);
db.connect();
db.deleteEntry("reb", "belegnummer", entryname.toString());
db.close();
// delete documents from ftp server as well....
// ......
refreshTable();
}
});
}
/////////////////////////////////////////////////////////////////
// refresh table
// //////////////////////////////////////////////////////////////
public static void refreshTable() {
String query = "SELECT * FROM reb where projectname like '" + year
+ "%' order by projectname";
Datenbank db = new Datenbank();
db.connect();
data = db.getBills(query);
db.close();
model = new DefaultTableModel(data, tableHeader) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(model);
model.fireTableDataChanged();
table.setRowSorter(sorter);
sorter.setModel(model);;
}
这是第一次创建JTable时的方法
public void getBills() {
String query = "SELECT * FROM reb where projektname like '" + year
+ "%' order by projektname";
Datenbank db = new Datenbank();
db.connect();
data = db.getBills(query);
db.close();
model = new DefaultTableModel(data, tableHeader) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new JTable(model);
sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
}
这里是例外
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at javax.swing.DefaultRowSorter.convertRowIndexToModel(Unknown Source)
at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
at buchungen.Overview$3.valueChanged(Overview.java:230)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(Unknown Source)
at javax.swing.DefaultListSelectionModel.clearSelection(Unknown Source)
at javax.swing.JTable.clearSelection(Unknown Source)
at javax.swing.JTable.clearSelectionAndLeadAnchor(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.JTable.setModel(Unknown Source)
at buchungen.Overview.refreshTable(Overview.java:501)
at buchungen.Overview$4.actionPerformed(Overview.java:456)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
【问题讨论】:
-
好吧,这里的任何建议都是关于猜测的,为了更好地帮助尽快发布 SSCCE,简短,可运行,可编译,为 TableModel 提供硬编码值以避免,减少来自 JDBC、FTP 的任何异常等
-
但其他一切都很好。程序在 'table.setModel(model);' 处引发异常。我只是不知道哪个数组越界,因为数组“数据”包含调试时数据库中的所有正确数据。
-
有时听起来像是不正确的同步;见Initial Threads。
标签: java swing exception jtable tablerowsorter