[英]Java JTable RowSorter throws exception
I really need your help...I'm already kind of desperate because my JTable updating doensn't work properly. 我真的需要您的帮助...我已经很拼命,因为我的JTable更新无法正常工作。
I got a JTable which receives its data from a database via mySQl. 我有一个JTable,它通过mySQl从数据库接收其数据。 I store the data in an array and pass it on to the tableModel. 我将数据存储在数组中,并将其传递给tableModel。 After fireing 'fireTableDatachanged' I see all the Data. 触发“ fireTableDatachanged”后,我将看到所有数据。 This works also when deleting a row: I just delete the entry on my database and read out the new data from the DB. 这在删除行时也起作用:我只是删除数据库中的条目,然后从数据库中读出新数据。
So here's the weird thing: Sometimes it works and sometimes not... I'm also using the RowSorter and this might be the actual problem. 所以这是一件奇怪的事情:有时它有用,有时不起作用...我也在使用RowSorter,这可能是实际的问题。
I'd really appreciate your help and thanks in advance! 非常感谢您的帮助,并在此先感谢!
This is the code for deleting and refreshing table data: 这是用于删除和刷新表数据的代码:
//////////////////////////////////////////////////////////////
// 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);;
}
this is the method when first created the JTable 这是第一次创建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);
}
here is the exception 这是例外
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)
for me its working fine... I am using vector to store data.. 对我来说,它的工作正常...我正在使用向量存储数据。
remove the line ,row = table.convertRowIndexToModel(row); 删除行,行= table.convertRowIndexToModel(row);
dont use setmodel method,, table model public class STOCKmodel extends DefaultTableModel { 不要使用setmodel方法,表模型公共类STOCKmodel扩展了DefaultTableModel {
String ColumnName[] = {"Item Code" ,"Item Name" ,"Uom","Unit_Price" ,"Opening_quantity " ,"Current_Received_quantity" ,"Current_Issued_Quantity", "Closing_Stock", "Closing_Value" ,"Minimum"};
String ColumnType[] = {"A" ,"B" ,"T" ,"C" ,"D" ,"E" , "F" , "G" , "R" ,"s" };
int iColumnWidth[] = {120 , 200 ,140 ,200 ,70 ,200 , 70 , 70 , 200 ,200 };
DecimalFormat df;
public STOCKmodel()
{
setDataVector(getRowData(),ColumnName);
}
public Class getColumnClass(int iCol)
{
return getValueAt(0,iCol).getClass();
}
public Object getValueAt(int row, int column)
{
//==================
df = new DecimalFormat("#.##");
//Closing_value column_N0 7
//Closing_Stock column_No 6
//Unit_Price column_No 2
//=====================
//=======================
if (column == 3)
{
//String h = getValueAt(row,5).toString();
//String j =getValueAt(row, 6).toString();
double i,d,t;
try{
String h = getValueAt(row,8).toString();
String j =getValueAt(row, 7).toString();
// String l =getValueAt(row, 7).toString();
i = Double.parseDouble(h);
d = Double.parseDouble(j);
if (d==0)
d=1;
//t = Double.parseDouble(l);
}
catch(NumberFormatException e)
{
i=0;
d=0;
t=0;
}
//================================================
//double sub =i*d;
//val = df.format(val);
String tot ;
//String.valueOf(int);
try{
//if(val==0)
//tot =null;
double val = i/d;
tot = Double.toString(val);
tot = df.format(val);
if(val==0)
tot ="nil";
//tot =String.valueOf(sub);
}
catch (ArithmeticException ae) {
tot ="nil";
}
catch(NumberFormatException e)
{
tot ="nil";
}
//================================================
return tot;
}
return super.getValueAt(row, column);
}
public boolean isCellEditable(int iRow,int iCol)
{
return false;
}
public boolean isCellEdit(int iRow,int iCol)
{
return true;
}
private Object[][] getRowData()
{
Object RowData[][] = new Object[1][ColumnName.length];
for(int i=0;i<ColumnName.length;i++)
RowData[0][i] = "";
return RowData;
}
public void appendRow(Vector theVect)
{
insertRow(getRows(),theVect);
}
public int getRows()
{
return super.dataVector.size();
}
public void appendEmptyRow()
{
Vector<Object> curVector = new Vector<Object>();
for(int i=0;i<ColumnName.length;i++) {
curVector.addElement(" ");
}
insertRow(getRows(),curVector);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.