JTable stop cell editing without user click

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.

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.

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.

I appreciate the help!


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

Check out out Table Stop Editing for two approaches:

You can either:

Add code to the ActionListener:

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

or set a property on the table:

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

Edit:

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

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

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

链接地址: http://www.djcxy.com/p/48826.html

上一篇: 在旁边的另一个JTable上继续JTable数据,而不是滚动

下一篇: JTable停止单元格编辑,无需用户单击