jesric1029
jesric1029

Reputation: 718

Saving JTable edits to an array

I know this question has been asked but after reading the other answers I still did not quite understand exactly what I need to do.

I have created a massive JTable (60 columns and a dynamic amount of rows) that holds data after being parsed from an XML file with a DOM Parser.

The idea is that the user should be able to edit the fields in the table and then the click a save button and a new XML will be generated reflecting the changes.

The problem is that when the user click saves and the action listener tells all the arrays/strings that had the original XML values copy all of the cell values, only the old cell values are copied even if they have been changed.

I have read other posts where people advised using a different table model but I'd really prefer not to do this if possible. As I've already finished the table I hate to backtrack and am hoping for an easier fix.

I'll attempt to shorten my code for your convenience.

public static int counter;
    public static int counter2;
    public static JTable tbl;
    public static int currentRow;

    public static void createGUI(){

        counter=XMLParser.NtryRefAL.size();
        counter2=counter;

        final JFrame frame = new JFrame("CamT54 Builder");
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel panelNorth = new JPanel(new BorderLayout());
        JPanel panelSouth = new JPanel(new BorderLayout());
        JPanel buttonPanel = new JPanel(new FlowLayout());

        JButton uploadButton = new JButton("Upload");
        uploadButton.setPreferredSize(new Dimension(150,40));
        buttonPanel.add(uploadButton);

        JButton saveButton = new JButton("Save Changes");
        saveButton.setPreferredSize(new Dimension(150,40));
        buttonPanel.add(saveButton);

        //Creates the JTable
        tbl = new JTable();
        DefaultTableModel dtm = new DefaultTableModel(0,0);
        String header[] = new String[]{//All 60 column headers are here};

            dtm.setColumnIdentifiers(header);

            tbl.setModel(dtm);




            for(int count = XMLParser.NtryRefAL.size(); count >0; count--){

                dtm.addRow(new Object[]{XMLParser.MsgRcptS,XMLParser.CreDtTmS,XMLParser.MsgIdS,
                //I add more values for each of the columns here, the loop is for each row.}


                XMLParser.NtryRefAL.remove(0);
                XMLParser.AmtCcyAL.remove(0);
                //Continue to remove the array values so they 
                            can be replaced with the cell values. 



            }

            tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


            tbl.getColumnModel().getColumn(0).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(1).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(2).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(3).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(4).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(5).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(6).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(7).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(8).setPreferredWidth(150);
            tbl.getColumnModel().getColumn(9).setPreferredWidth(150);
            //Set the rest of the column widths..

            uploadButton.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent ae){

                    FileHandling fHandling = new FileHandling();
                    fHandling.getFile();
                    XMLParser parse = new XMLParser();
                    parse.parseXML();
                    frame.dispose();
                    MainGui.createGUI();
                }
            });

            saveButton.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent ae){

                    String value = (String) tbl.getValueAt(0, 0);
                    currentRow=0;


                    //Here I want to save all the cell values into array lists from my other class. 
                    while(counter2>0){
                        System.out.println(tbl.getValueAt(0, 0));
                        XMLParser.MsgRcptS.equals(tbl.getValueAt(currentRow, 0));
                        XMLParser.CreDtTmS.equals(tbl.getValueAt(currentRow, 1));
                        XMLParser.MsgIdS.equals(tbl.getValueAt(currentRow, 2));
                        XMLParser.NtfctnIdS.equals(tbl.getValueAt(currentRow, 3));
                        XMLParser.NtfctnCreDtTmS.equals(tbl.getValueAt(currentRow, 4));
                        XMLParser.AcctIdS.equals(tbl.getValueAt(currentRow, 5));
                        XMLParser.NbOfNtriesS.equals(tbl.getValueAt(currentRow, 6));
                        XMLParser.SumS.equals(tbl.getValueAt(currentRow, 7));
                        XMLParser.StsAL.add((String) tbl.getValueAt(currentRow, 8));
                        XMLParser.NtryRefAL.add((String) tbl.getValueAt(currentRow, 9));
                        XMLParser.RsvlIndAL.add((String) tbl.getValueAt(currentRow, 10));
                        XMLParser.CdtDbtIndAL.add((String) tbl.getValueAt(currentRow, 11));
                        XMLParser.AddtlNtryInfAL.add((String) tbl.getValueAt(currentRow, 12));
                        XMLParser.AmtCcyAL.add((String) tbl.getValueAt(currentRow, 13));
                        XMLParser.TxDtlsAmtAL.add((String) tbl.getValueAt(currentRow, 14));
                        XMLParser.TxDtlsCdtDbtIndAL.add((String) tbl.getValueAt(currentRow, 15));
                        XMLParser.BkTxCdAL.add((String) tbl.getValueAt(currentRow, 16));
                        XMLParser.TxDtlsMsgIdAL.add((String) tbl.getValueAt(currentRow, 17));
                        XMLParser.PmtInfIdAL.add((String) tbl.getValueAt(currentRow, 18));
                        XMLParser.InstrIdAL.add((String) tbl.getValueAt(currentRow, 19));
                        XMLParser.EndToEndIdAL.add((String) tbl.getValueAt(currentRow, 20));
                        XMLParser.TxIdAL.add((String) tbl.getValueAt(currentRow, 21));
                        XMLParser.InitgPtyIdAL.add((String) tbl.getValueAt(currentRow, 22));
                        XMLParser.DbtrAL.add((String) tbl.getValueAt(currentRow, 23));
                        XMLParser.DbtrAcctPrtryAL.add((String) tbl.getValueAt(currentRow, 24));
                        XMLParser.DbtrAcctIdAL.add((String) tbl.getValueAt(currentRow, 25));
                        XMLParser.CdtrIdAL.add((String) tbl.getValueAt(currentRow, 26));
                        XMLParser.CdtrAcctIdAL.add((String) tbl.getValueAt(currentRow, 27));
                        XMLParser.ClrSysIdCdAL.add((String) tbl.getValueAt(currentRow, 28));
                        XMLParser.MmbIdAL.add((String) tbl.getValueAt(currentRow, 29));
                        XMLParser.CdtrAgtClrSysIdCdAL.add((String) tbl.getValueAt(currentRow, 30));
                        XMLParser.CdtrAgtMmbIdAL.add((String) tbl.getValueAt(currentRow, 31));
                        XMLParser.FinInstnIdNmAL.add((String) tbl.getValueAt(currentRow, 32));
                        XMLParser.RfrdDocInfNbAL.add((String) tbl.getValueAt(currentRow, 33));
                        XMLParser.RfrdDocInfTpAL.add((String) tbl.getValueAt(currentRow, 34));
                        XMLParser.AdjstmntAmtAndRsnAmtCcyAL.add((String) tbl.getValueAt(currentRow, 35));
                        XMLParser.AdjstmntAmtAndRsnCdtDbtIndAL.add((String) tbl.getValueAt(currentRow, 36));
                        XMLParser.AdjstmntAmtAndRsnAddtlInfAL.add((String) tbl.getValueAt(currentRow, 37));
                        XMLParser.CdtrRefInfTpAL.add((String) tbl.getValueAt(currentRow, 38));
                        XMLParser.CdtrRefInfRefAL.add((String) tbl.getValueAt(currentRow, 39));
                        XMLParser.IntrBkSttlmDtAL.add((String) tbl.getValueAt(currentRow, 40));
                        XMLParser.TxDtTmAL.add((String) tbl.getValueAt(currentRow, 41));
                        XMLParser.RmtCaptureDtAL.add((String) tbl.getValueAt(currentRow, 42));
                        XMLParser.RmtRcvdDtAL.add((String) tbl.getValueAt(currentRow, 43));
                        XMLParser.RmtProcDtAL.add((String) tbl.getValueAt(currentRow, 44));
                        XMLParser.PayInstrmntAL.add((String) tbl.getValueAt(currentRow, 45));
                        XMLParser.PayTpCdAL.add((String) tbl.getValueAt(currentRow, 46));
                        XMLParser.PrimaryCustIdAL.add((String) tbl.getValueAt(currentRow, 47));
                        XMLParser.EnrollmentIdAL.add((String) tbl.getValueAt(currentRow, 48));
                        XMLParser.CardAcctTpAL.add((String) tbl.getValueAt(currentRow, 49));
                        XMLParser.PassThruDataAL.add((String) tbl.getValueAt(currentRow, 50));
                        XMLParser.RmtRepresmtDtAL.add((String) tbl.getValueAt(currentRow, 51));
                        XMLParser.RmtFinalDtAL.add((String) tbl.getValueAt(currentRow, 52));
                        XMLParser.RmtSettledDtAL.add((String) tbl.getValueAt(currentRow, 53));
                        XMLParser.RmtPaidDtAL.add((String) tbl.getValueAt(currentRow, 54));
                        XMLParser.ClrgMopAL.add((String) tbl.getValueAt(currentRow, 55));
                        XMLParser.ClrgAcctAL.add((String) tbl.getValueAt(currentRow, 56));
                        XMLParser.JobIdAL.add((String) tbl.getValueAt(currentRow, 57));
                        XMLParser.NachaTransCdAL.add((String) tbl.getValueAt(currentRow, 58));
                        XMLParser.TransTpAL.add((String) tbl.getValueAt(currentRow, 59));


                        System.out.println(XMLParser.NtryRefAL);
                        currentRow++;
                        counter2--;

                    }

                    WriteParser wParser = new WriteParser();
                    wParser.reWrite();
//Rewrites the XML values with the new array list values. This part works 
//it just doesn't write the values I want because they aren't being saved. 


                }

            });


            panelNorth.add(tbl);
            panelNorth.add(new JScrollPane(tbl));
            panelNorth.setPreferredSize(new Dimension(500,500));
            panelSouth.add(buttonPanel,BorderLayout.NORTH);
            mainPanel.add(panelNorth,BorderLayout.NORTH);
            mainPanel.add(panelSouth,BorderLayout.SOUTH);
            frame.add(mainPanel);

            frame.setVisible(true);

            frame.setSize(1900,600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Sorry if that code was difficult to read. I believe the real root of my problem lies here:

XMLParser.BkTxCdAL.add((String) tbl.getValueAt(currentRow, 16));

None of those codes work. They simply pull the old value, not the new one. How can I fix this?

Upvotes: 0

Views: 69

Answers (2)

jesric1029
jesric1029

Reputation: 718

After doing more searching I finally found a solution that worked. The problem was that my table was still set as cell editing being turned on.

Adding this:

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

Before attempting to save all my cells into the arrays worked perfectly.

Upvotes: 1

Horacio Garza
Horacio Garza

Reputation: 135

Try to use the toString() method instead of a cast, as I remember, a String cast doesn't works when a value is predifened

Upvotes: 0

Related Questions