【问题标题】:Saving JTable edits to an array将 JTable 编辑保存到数组
【发布时间】:2016-02-05 00:41:32
【问题描述】:

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

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

这个想法是用户应该能够编辑表中的字段,然后单击保存按钮,将生成一个反映更改的新 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));

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

【问题讨论】:

  • 我敢打赌,如果您将此程序简化为MVCE,问题将直接跳出。没有 60 列或 100 行等。
  • 确实如此,但我想不出一种容易做到这一点的方法,因为它已经有很多类了。我发布了最后一行代码以准确显示问题所在。我只需要知道如何将编辑后的单元格值保存到数组中。
  • AFAICT 最后一行不是问题。编辑单元格时,更改将传播到模型。如果你稍后检索一个单元格,你应该得到一个更新的值。

标签: java swing jtable


【解决方案1】:

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

添加这个:

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

在尝试将我的所有单元格保存到数组中之前效果很好。

【讨论】:

  • 那么肯定有什么问题,阻止了super.setValueAt(DefaultTableModel中的默认值)
  • 谢谢 TT,不过 2 天做不到 :(
【解决方案2】:

尝试使用 toString() 方法而不是强制转换,我记得,当预先定义值时,字符串强制转换不起作用

【讨论】:

  • 谢谢,但这不起作用。问题是表的值仍然设置为原始值。一个人可以“编辑”它们,但实际值仍然相同,只是文本看起来不同。
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多