繁体   English   中英

JTable 无需用户单击即可停止单元格编辑

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM