Jtable setValueAt 变得无关紧要

Jtable setValueAt is made irrelevant

我成功制作了一个 Jtable,它使用 CachedRowSet 从数据库加载。我将 table 放在 jframe 中,然后在按下按钮时调用 Ui。

当我尝试更改单元格中的值时,它们会自动重置为原始值,我不知道为什么。我认为这与 AbstractTableModel 有关,因为当我使用

方法加载 table 时

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

我没有这个问题。

我正在尝试将 Jtable 中的值更新到数据库中,我刚刚注意到了这种行为。如果有人能向我解释为什么这样做以及如何禁用它,我将不胜感激 属性。

这里是为什么table模型的代码:

  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) {
  }

  }

在我的 JFrame 构造函数中,我这样初始化 table:

 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);

*********************编辑************************

所以我尝试实现这个方法,它有点管用,但现在每次我单击一个单元格时,它都会自动以随机方式将值插入剪贴板。有人可以告诉我我做错了什么并告诉我实现此方法的更好方法吗?谢谢

  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);

 }
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);
  }