Question

I really need your help...I'm already kind of desperate because my JTable updating doensn't work properly.

I got a JTable which receives its data from a database via mySQl. I store the data in an array and pass it on to the tableModel. After fireing 'fireTableDatachanged' I see all the Data. 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.

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

    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)

No correct solution

OTHER TIPS

for me its working fine... I am using vector to store data..

remove the line ,row = table.convertRowIndexToModel(row);

dont use setmodel method,, table model 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);
 }

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top