0

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

1 回答 1

0

对我来说它工作正常......我正在使用矢量来存储数据..

删除行 ,row = table.convertRowIndexToModel(row);

不要使用 setmodel 方法,表模型 public class STOCKmodel extends 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);
 }

 }
于 2015-09-03T11:05:29.947 回答