Jtable setValueAt变得无关紧要

时间:2015-01-19 01:56:56

标签: java swing jtable abstracttablemodel cachedrowset

我成功地创建了一个使用CachedRowSet从数据库加载的Jtable。我把桌子放在一个jframe中,按下按钮时调用Ui。

jtable

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

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

我没有遇到这个问题。

我试图将值从Jtable更新到数据库,我只是注意到了这种行为。如果有人可以向我解释为什么会这样做以及如何禁用此属性,我真的很感激。

以下是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) {
  }

  }

在我的JFrame构造函数中,我按原样初始化表:

 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 ********

所以我尝试实现这种方法它有点工作,但现在每次我点击一个单元格时,它会自动以随机的方式将值插入剪贴板。有人能告诉我我做错了什么并向我展示了实施这种方法的更好方法吗?感谢

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

 }

1 个答案:

答案 0 :(得分:1)

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