jes*_*029 1 java swing jtable tablecelleditor
我正在尝试用我的程序解决一个奇怪的问题。该程序创建了一系列GUI和JTable,使用户能够生成XML文件。这些表之一是用于创建“语句”的。除了将数据存储在多个2D数组中(依次存储在哈希图中)之外,我将不做进一步的详细介绍。
这是发生了什么。当用户进入Statement屏幕时,将使用2D数组中的内容生成JTable。该数据填充用户能够修改的单元格。这些单元格之一(也是最重要的)是数量。他们为行设置的数量与另一个类的其他数量非常匹配。
表格底部是一个“完成”按钮。当用户单击此按钮时,逻辑将检查金额是否匹配。如果这样做,则程序将使用任何更改的值更新2D数组并处理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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
我认为我的问题在于这条线:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
Run Code Online (Sandbox Code Playgroud)
仅当用户在编辑单元格后单击表的另一个区域时,这才似乎起作用。
感谢您的帮助!
我的问题是,一旦用户更新了单元并单击“完成”,最后所做的更新将不起作用。
查看表停止编辑的两种方法:
您可以:
将代码添加到ActionListener:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
Run Code Online (Sandbox Code Playgroud)
或在表格上设置属性:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
编辑:
当用户单击完成时,所有单元格数据将保存到2D阵列中。
为什么?所有数据已存储在TableModel中。
无论如何,在尝试将数据从TableModel复制到Array之前,都需要停止编辑。