[英]JTable stop cell editing without user click
我正在嘗試用我的程序解決一個奇怪的問題。 該程序創建了一系列 GUI 和 JTable,使用戶能夠生成 XML 文件。 這些表之一用於創建“語句”。 除了說數據存儲在多個 2D 數組中,而這些 2D 數組又存儲在哈希映射中之外,我不會詳細介紹。
這是發生的事情。 當用戶進入 Statement 屏幕時,將使用 2D 數組中的內容生成一個 JTable。 此數據填充用戶能夠修改的單元格。 這些單元格之一(也是最重要的)是數量。 他們為行設置的數量與另一個類別的另一個數量非常匹配。
在表格的底部是一個“完成”按鈕。 當用戶單擊此按鈕時,邏輯將檢查金額是否匹配。 如果他們這樣做,那么程序將使用任何更改的值更新二維數組並處理 JTable。
我的問題是,一旦用戶更新單元格並單擊“完成”,最后所做的更新就不起作用。 本質上,用戶必須首先點擊表格中的其他地方,然后點擊完成。 我希望此操作自動發生,以便在用戶單擊“已完成”單元格編輯時停止。 這是完成按鈕的代碼:
finishedButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//Creates another table model for the finished button logic.
DefaultTableModel dm = (DefaultTableModel)StatementGUI.tbl.getModel();
//Gets the total number of table rows.
int rows = dm.getRowCount();
//Creates a variable to store the statement transaction total.
double statementTransactionTotal=0;
//For each row in the table.
for(int i = 0; i < dm.getRowCount(); i++){
//Gets the total of all transactions in the table.
String currentTotal = tbl.getValueAt(i, 3).toString();
Double currentTotalDouble = Double.parseDouble(currentTotal);
statementTransactionTotal=statementTransactionTotal+currentTotalDouble;
}
//Creates a decimal format and applies the statement total.
DecimalFormat df = new DecimalFormat("0.00");
String currentTotalDF = df.format(statementTransactionTotal);
//Stops editing on the table so that the data can be used.
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
//If the statement total matches the transaction total..
if(currentTotalDF.matches(ClearedMainGUI.currentTransactionAmount)){
//For each row in the table..
for(int i = 0; i < dm.getRowCount(); i++){
//Will replace the hash/array value with the table value.
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][0]=tbl.getValueAt(i, 0).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][1]=tbl.getValueAt(i, 1).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][2]=tbl.getValueAt(i, 2).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][3]=tbl.getValueAt(i, 3).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][4]=tbl.getValueAt(i, 4).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][5]=tbl.getValueAt(i, 5).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][6]=tbl.getValueAt(i, 6).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][7]=tbl.getValueAt(i, 7).toString();
}
//For each row in the table..
for(int i = rows - 1; i >=0; i--){
//Removes the current row so the table will be empty next time.
dm.removeRow(i);
}
//Removes the frame and goes back to the previous GUI.
frame.dispose();
//If the statement total and transaction total do not match..
}else{
JOptionPane.showMessageDialog(null, "The statement total entered: $"+statementTransactionTotal+" " +
"does not match the transaction total of: $"+ClearedMainGUI.currentTransactionAmount);
}
}
});
我認為我的問題出在這一行:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
只有當用戶在編輯單元格后單擊表格的另一個區域時,這似乎才起作用。
我感謝您的幫助!
我的問題是,一旦用戶更新單元格並單擊“完成”,最后所做的更新就不起作用。
查看Table Stop Editing以了解兩種方法:
您可以:
向 ActionListener 添加代碼:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
或在桌子上設置一個屬性:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
編輯:
當用戶單擊完成時,所有單元格數據將保存到二維數組中。
為什么? 所有數據都已經存儲在 TableModel 中。
在任何情況下,您都需要在嘗試將數據從 TableModel 復制到數組之前停止編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.