我有一个带有列表选择侦听器的 jtable 我可以动态地将新行添加到我的表中,当我选择行时,选定的行内容将出现在文本框中我可以编辑和删除数据,对于我的应用程序我存储了表数据到 xml 文件中,当我添加新行时,将成功添加到表中。但是当我选择一行并且更新意味着表没有得到更新(这里我调用加载表())。(但更新的值在 xml 文件中正确更改)这是我创建表的示例代码*
ListSelectionModel selectionModel;
JTable table1;
model = new DefaultTableModel();
table = new JTable(model); table.setRowHeight(20);
selectionModel = table.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
stxtBox.setText("");
ptxtBox.setText("");
ntxtBox.setText("");
if (!e.getValueIsAdjusting()) {
model1 = table.getSelectionModel();
int lead = model1.getLeadSelectionIndex();
int columns = table.getColumnCount();
String sip = "";
String sport = "";
String snoq = "";
for (int col = 0; col < columns; col++) {
Object o = table.getValueAt(lead, col);
if (col == 0) {
sip += o.toString();
stxtBox.setText(sip);
selectedip = sip;
} else if (col == 1) {
sport += o.toString();
ptxtBox.setText(sport);
selectedport = sport;
} else {
snoq += o.toString();
ntxtBox.setText(snoq);
}
selectedreq = snoq;
}
}table.clearSelection();
}
});
我像这样加载表格内容
int rowCount=0;
File file = new File("serverconfig.xml");
if (file.exists())
{
System.out.print("in load");
int e = table.getRowCount();
if(e> 0)
{
while (table.getRowCount() > 0) {
((DefaultTableModel) table.getModel()).removeRow(0);
}
}
//here i will load table content from my xml file (that's working fine)
问题是当我更新我的表格内容时,我会调用 loadtable() 函数,每次我都会收到这个错误
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at Testsample$18.valueChanged(Testsample.java:1810)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.insertIndexInterval(Unknown Source)
at javax.swing.JTable.tableRowsInserted(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at Testsample.loadtable(Testsample.java:577)
at Testsample$10.actionPerformed(Testsample.java:1551)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)