当我运行一个可能在那里删除行的方法时,当我尝试清除表行时,抛出表格的标题异常。谁能解释我为什么会这样?我不是java的专家。
例外:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Vector.java:430)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at javax.swing.JTable.getValueAt(JTable.java:2652)
at classes.RowListener.displayRowValues(RowListener.java:40)
at classes.RowListener.valueChanged(RowListener.java:32)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:660)
at javax.swing.JTable.tableRowsDeleted(JTable.java:4460)
at javax.swing.JTable.tableChanged(JTable.java:4363)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:245)
at javax.swing.table.DefaultTableModel.setNumRows(DefaultTableModel.java:304)
at javax.swing.table.DefaultTableModel.setRowCount(DefaultTableModel.java:322)
at saraentry.MainFace.clearTable(MainFace.java:73)
at saraentry.MainFace.jButton1ActionPerformed(MainFace.java:578)
at saraentry.MainFace.access$1200(MainFace.java:32)
at saraentry.MainFace$14.actionPerformed(MainFace.java:424)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 20 seconds)
代码:
public class MainFace extends javax.swing.JFrame {
public String selectedRaw, tColSqNum, tColJobNo, tColDecName, tColDate;
String pendingDate, getTDate;
/** Creates new form MainFace */
public MainFace() {
theme.theme();
initComponents();
newJob.setMnemonic(KeyEvent.VK_N);
setLocationRelativeTo(null);
DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
tm.setRowCount(0);
ListSelectionModel selectionModel = jTable1.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectionModel.addListSelectionListener(new RowListener(this));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date Sysdate = new Date();
lblDate.setText(dateFormat.format(Sysdate));
jXDatePicker1.setFormats(dateFormat);
Jobs();
}
public void Jobs() {
try {
Connection c = DB.myConnection();
ResultSet rs = c.createStatement().executeQuery("SELECT * FROM totjobno WHERE ID='todayjob' AND date=('" + lblDate.getText().trim() + "')");
while (rs.next()) {
String x = rs.getString("TotJobNo");
lblCuJobNo.setText(x);
}
c.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void clearTable() {
}
private void pendingList2(String getDate) {
try {
Connection c = DB.myConnection();
Statement s = c.createStatement();
DefaultTableModel tm33 = (DefaultTableModel) jTable1.getModel();
tm33.setRowCount(0);
ResultSet r = s.executeQuery("SELECT * FROM newjobsheet WHERE date=('" + getDate + "') ORDER BY sqNumber ");
while (r.next()) {
Vector v = new Vector();
v.addElement(r.getString("sqNumber"));
v.addElement(r.getString("date"));
v.addElement(r.getString("JobNumber"));
v.addElement(r.getString("DecName"));
tm33.addRow(v);
}
} catch (ArrayIndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(this, "Press OK!!", "Error!", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}
private void btnAllPendingActionPerformed(java.awt.event.ActionEvent evt) {
jXDatePicker1.setDate(null);
try {
DefaultTableModel tm33 = (DefaultTableModel) jTable1.getModel();
tm33.setRowCount(0);
Connection c = DB.myConnection();
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM newjobsheet ORDER BY sqNumber ");
while (r.next()) {
Vector v2 = new Vector();
v2.addElement(r.getString("sqNumber"));
v2.addElement(r.getString("date"));
v2.addElement(r.getString("JobNumber"));
v2.addElement(r.getString("DecName"));
tm33.addRow(v2);
}
} catch (ArrayIndexOutOfBoundsException e) {
JOptionPane.showMessageDialog(this, "Press OK!!", "Error!", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
请检查以下内容:
classes.RowListener.displayRowValues(RowListener.java:40)在class.RowListener.valueChanged(RowListener.java:32)at
他们似乎是你自己的班级。
特别是如果在调用setRowCount(0)之前选择表中的任何行,则可能会生成第一个或最后一个索引为-1的选择更改事件。
因此,您应该在继续进行之前添加对索引有效值的检查。
如果您运行以下代码并选择表中的第二行,您将看到以-1作为第一个索引触发选择事件。所以你应该在RowListener中防范它。
public static void main(String[] args) throws Exception
{
JTable table = new JTable();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("A");
model.addRow(new String[]{"A"});
model.addRow(new String[]{"A"});
JFrame f = new JFrame();
f.add(table);
f.pack();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e)
{
System.out.println(e.getFirstIndex() + ":" + e.getLastIndex());
}
});
Thread.sleep(10000);
model.setRowCount(0);
}
答案 1 :(得分:1)
1)在打开Connection之前创建DefaultTableModel,并且在数据库操作之前也应该调用代码行tm33.setRowCount(0);
2)从try - catch块中删除
- `DefaultTableModel tm33 = (DefaultTableModel) jTable1.getModel();`
- `tm33.setRowCount(0);`
3)将finally块添加到try-catch中,然后在最后的shopuld中关闭
Connection c = DB.myConnection();
Statement s = c.createStatement();
如果对数据库的查询是短期和周期性的,那么也不需要关闭连接,