java - 将 JTable 编辑保存到数组

标签 java swing jtable

我知道有人问过这个问题,但在阅读其他答案后,我仍然不太明白我需要做什么。

我创建了一个巨大的 JTable(60 列和动态数量的行),用于保存使用 DOM 解析器从 XML 文件解析后的数据。

这个想法是,用户应该能够编辑表中的字段,然后单击保存按钮,将生成反射(reflect)更改的新 XML。

问题是,当用户单击“保存”并且操作监听器告诉所有具有原始 XML 值的数组/字符串复制所有单元格值时,即使旧单元格值已更改,也只会复制旧单元格值。

我读过其他帖子,人们建议使用不同的表模型,但如果可能的话,我真的不想这样做。因为我已经完成了表格,所以我不想回溯并希望有一个更简单的解决方案。

为了您的方便,我将尝试缩短我的代码。

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

}

如果该代码难以阅读,我们深表歉意。我相信我的问题的真正根源在于:

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

这些代码都不起作用。他们只是拉旧值,而不是新值。我怎样才能解决这个问题?

最佳答案

经过更多搜索,我终于找到了一个有效的解决方案。问题是我的表格仍然设置为打开单元格编辑。

添加此内容:

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

在尝试将所有单元格保存到数组中之前,效果非常好。

关于java - 将 JTable 编辑保存到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205371/

相关文章:

java - volatile 数组 - 元素的内存可见性

java - 需要返回数组列表的平均值

java - 如果所有人的工资总额超过...,如何制作职位列表?

java - 尽管变量的类型被声明为 double ,为什么 java 会给出一个 int too large 错误?

java - 通过循环添加 JButton 会导致 ArrayIndexOutOfBoundsException

java - 如何向表添加监听器?

java - 增加在 JList 中输入和查找元素的时间

java - Swing 中使用 AbstractListModel 的大型虚拟列表——限制为 119,000,000 个项目?

java - 在 JTable 中显示 Unicode 字符串

Java - 使用另一个类的数据填充 Jtable