gpt4 book ai didi

java - Jtable setValueAt 变得无关紧要

转载 作者:行者123 更新时间:2023-12-01 12:00:26 29 4
gpt4 key购买 nike

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

jtable

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

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

我没有这个问题。

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

这是为什么表模型的代码:

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

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

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

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

关于java - Jtable setValueAt 变得无关紧要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28016928/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com