我正在尝试用我的程序解决一个奇怪的问题 . 该程序创建了一系列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);
}
}
});
我认为我的问题在于这一行:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
只有在用户在编辑单元格后单击表格的另一个区域时,这似乎才有效 .
我很感激帮助!