javaswingjtableabstracttablemodelcachedrowset

Jtable setValueAt is made irrelevant


I succeeded in making a Jtable which loads from the database using CachedRowSet. I placed the table in a jframe and call the Ui when pressing a button.

jtable

When I try to change the values in the cell, they are automatically reset to their original value, I wonder why. I think it has to do with the AbstractTableModel because when I'm loading the table using the method

TicketLine.setModel(DbUtils.resultSetToTableModel(tline_rs));

I don't have this problem.

I'm trying to update values from the Jtable to the database and I just noticed this behavior. I'd really appreciate if someone could explain to me why it does so and how to disable this property.

Here is the code of why tablemodel:

  public class CoffeesTableModel extends AbstractTableModel {

  CachedRowSet coffeesRowSet; // The ResultSet to interpret
  ResultSetMetaData metadata; // Additional information about the results
  int numcols, numrows; // How many rows and columns in the table

  public CachedRowSet getCoffeesRowSet() {
    return coffeesRowSet;
  }


  public CoffeesTableModel(CachedRowSet rowSetArg) throws SQLException {

    this.coffeesRowSet = rowSetArg;
    this.metadata = this.coffeesRowSet.getMetaData();
    numcols = metadata.getColumnCount();

    // Retrieve the number of rows.
    this.coffeesRowSet.beforeFirst();
    this.numrows = 0;
    while (this.coffeesRowSet.next()) {
      this.numrows++;
    }
    this.coffeesRowSet.beforeFirst();
  }

  public void addEventHandlersToRowSet(RowSetListener listener) {
//    this.coffeesRowSet.addRowSetListener(listener);
  }


  public void insertRow(String coffeeName, int supplierID, float price,
                        int sales, int total) throws SQLException {

//    try {
//      this.coffeesRowSet.moveToInsertRow();
//      this.coffeesRowSet.updateString("COF_NAME", coffeeName);
//      this.coffeesRowSet.updateInt("SUP_ID", supplierID);
//      this.coffeesRowSet.updateFloat("PRICE", price);
//      this.coffeesRowSet.updateInt("SALES", sales);
//      this.coffeesRowSet.updateInt("TOTAL", total);
//      this.coffeesRowSet.insertRow();
//      this.coffeesRowSet.moveToCurrentRow();
//    } catch (SQLException e) {
//        JOptionPane.showMessageDialog(null, e);
//    }
  }

  public void close() {
    try {
      coffeesRowSet.getStatement().close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e);
    }
  }

  /** Automatically close when we're garbage collected */
  protected void finalize() {
    close();
  }


  public int getColumnCount() {
    return numcols;
  }


  public int getRowCount() {
    return numrows;
  }



  public String getColumnName(int column) {
    try {
      return this.metadata.getColumnLabel(column + 1);
    } catch (SQLException e) {
      return e.toString();
    }
  }



  public Class getColumnClass(int column) {
    return String.class;
  }



  public Object getValueAt(int rowIndex, int columnIndex) {

    try {
      this.coffeesRowSet.absolute(rowIndex + 1);
      Object o = this.coffeesRowSet.getObject(columnIndex + 1);
      if (o == null)
        return null;
      else
        return o.toString();
    } catch (SQLException e) {
      return e.toString();
    }
  }



  public boolean isCellEditable(int rowIndex, int columnIndex) {
//   if((columnIndex==2)|| (columnIndex ==4)){
       return true;
//   }else{
//           return false;
//   }
  }


  public void setValueAt(Object value, int row, int column) {
    System.out.println("Calling setValueAt row " + row + ", column " + column + ", Value :"+ value);
    //get the values set and check if it was changed
    //if value was changed, set corresponding price
    //if value was changed push it to the database
//    Object[][] rowData;
//    rowData [row][column] = value ;
       fireTableCellUpdated(row, column);



  }


  public void addTableModelListener(TableModelListener l) {
  }

  public void removeTableModelListener(TableModelListener l) {
  }

  }

In my JFrame constructor i initialize the table as such:

 CachedRowSet myCachedRowSet = getContentsOfCoffeesTable();
    myCoffeesTableModel = new CoffeesTableModel(myCachedRowSet);
    myCoffeesTableModel.addEventHandlersToRowSet(this);
    Jtable.setModel(myCoffeesTableModel);//sets the ticket line table
    Jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);//save cell content on focus lost
    Jtable.setSurrendersFocusOnKeystroke(true);

*******************EDIT***********************

So I tried to implement this method it kinda works but now each time I click on a cell it automatically insert the value in the clipboard in a random manner. Can someone tell me what I'm doing wrong and show me a better way of implementing this method? thanks

  public void setValueAt(Object value, int row, int column) {
  System.out.println("Calling setValueAt row " + row + ", column " +  column + ", Value :"+ value);
   try {
      //get the values set and update cacheRowSet
           while(this.coffeesRowSet.next())
          coffeesRowSet.updateObject((column + 1), value);
          coffeesRowSet.updateRow();
          coffeesRowSet.refreshRow();

  } catch (SQLException ex) {
      Logger.getLogger(CoffeesTableModel.class.getName()).log(Level.SEVERE, null, ex);
  }

      fireTableCellUpdated(row, column);

 }

Solution

  • public void setValueAt(Object value, int row, int column) {
      try {
     //get the value  and update cacheRowSet
      this.coffeesRowSet.absolute( row + 1 );
      this.coffeesRowSet.updateObject((column + 1), value);
      this.coffeesRowSet.updateRow();
      this.coffeesRowSet.setTableName("TICKETLINES");
    
      this.coffeesRowSet.acceptChanges(s.getConnection());
      this.coffeesRowSet.refreshRow();
      } catch (SQLException ex) {
      Logger.getLogger(CoffeesTableModel.class.getName()).log(Level.SEVERE, null, ex);
      }
      fireTableCellUpdated(row, column);
      }