我知道这个问题已被提出但在阅读完其他答案后,我仍然不太了解我需要做什么。
我创建了一个巨大的JTable(60列和动态数量的行),在使用DOM Parser从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));
这些代码都不起作用。他们只是拉旧值,而不是新值。我怎样才能解决这个问题?
答案 0 :(得分:1)
在做了更多搜索后,我终于找到了一个有效的解决方案。问题是我的表仍然设置为打开单元格编辑。
添加:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
在尝试将所有单元格保存到阵列之前,工作完美。
答案 1 :(得分:0)
尝试使用toString()方法而不是强制转换,我记得,当一个值被预先赋值时,String强制转换不起作用