我真的需要你的帮助...我已经有点绝望了,因为我的 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)