JTable无需用户单击即可停止单元格编辑 [英] JTable stop cell editing without user click

查看:175
本文介绍了JTable无需用户单击即可停止单元格编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用我的程序解决一个奇怪的问题.该程序创建了一系列GUI和JTable,使用户能够生成XML文件.这些表之一是用于创建语句"的.除了要说数据存储在多个2D数组中外,这些2D数组又存储在哈希映射中.

I'm trying to solve a strange problem with my program. This program creates a series of GUI's and JTables that give a user the ability to generate an XML file. One of these tables is for creating the "statements". I won't get into detail as far as that except to say that the data is stored in multiple 2D arrays which are in turn stored in a hash map.

这是发生了什么.当用户进入Statement屏幕时,将使用2D数组中的内容生成JTable.此数据填充用户能够修改的单元格.这些单元格之一(也是最重要的)是数量.他们为行设置的数量与另一个类的其他数量非常匹配.

Here is what happens. When a user enters the Statement screen a JTable is generated using the contents from the 2D array. This data populates the cell's which the user is able to modify. One of these cells (and the most important) is the amount. The amounts they set for the rows much match another amount from another class.

表格底部是一个完成"按钮.当用户单击此按钮时,逻辑将检查金额是否匹配.如果这样做,程序将使用任何更改后的值更新2D数组并处理JTable.

At the bottom of the table is a "finished" button. When the user clicks this button the logic will check to see if the money amounts match. If they do then the program will update the 2D array with any changed values and dispose of the JTable.

我的问题是,一旦用户更新了单元格并单击完成",最后所做的更新将不起作用.本质上,用户必须首先单击表中的其他位置,然后单击完成".我希望此操作自动发生,以便在用户单击完成"的单元格时停止编辑.这是完成按钮的代码:

My problem is that once a user updates a cell and clicks "finished" the last updates made do not work. Essentially the user must first click somewhere else in the table and THEN hit finished. I would like this action to happen automatically so that when the user clicks "finished" cell editing is stopped. Here is the code for the finished button:

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

                }



            }

        });

我认为我的问题出在这一行:

I think my problem is with this line:

if(null != tbl.getCellEditor()){
                    tbl.getCellEditor().stopCellEditing();
                }

这似乎只有在用户在编辑单元格后单击表的另一个区域后才起作用.

This only seems to work once the user has clicked another area of the table after editing a cell.

感谢您的帮助!

推荐答案

我的问题是,一旦用户更新单元格并单击完成",最后所做的更新将不起作用.

My problem is that once a user updates a cell and clicks "finished" the last updates made do not work.

查看表格停止编辑的两种方法:

您可以:

将代码添加到ActionListener:

Add code to the ActionListener:

if (table.isEditing())
     table.getCellEditor().stopCellEditing();

或在表格上设置属性:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

当用户单击完成时,所有单元格数据将保存到2D阵列中.

When the user clicks finished all the cell data will be saved to a 2D array.

为什么?所有数据已经​​存储在TableModel中.

Why? All the data is already stored in the TableModel.

无论如何,在尝试将数据从TableModel复制到数组之前,都需要停止编辑.

In any case you need to stop editing BEFORE you attempt to copy data from the TableModel to the Array.

这篇关于JTable无需用户单击即可停止单元格编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆