当我删除JTable行时,为什么会出现ArrayIndexOutOfBoundsException?

时间:2015-09-16 14:51:06

标签: java swing jtable indexoutofboundsexception

当我尝试从JTable中删除一行时,我得到Connection

.ArrayIndexOutOfBoundsException

首先,我选择要删除的行,然后当我触发操作时,删除按钮会冻结一点并给我异常。

这是我的完整代码:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementData(Vector.java:734)
    at java.util.Vector.elementAt(Vector.java:477)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
    at Test$1.valueChanged(Test.java:34)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
    at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
    at javax.swing.JTable.tableRowsDeleted(JTable.java:4509)
    at javax.swing.JTable.tableChanged(JTable.java:4412)
    at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
    at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
    at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
    at Test$2.actionPerformed(Test.java:39)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

完整堆栈跟踪:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class Test extends JFrame{
    private static JTable table;
    private DefaultTableModel model = new DefaultTableModel();;
    private JButton del = new JButton("DELETE");
    public Test(){
        JPanel pan= new JPanel();
        JTextField numeField = new JTextField(30);
        model.addColumn("Id");
        model.addColumn("Nume");
        table = new JTable(model);
        JScrollPane pen = new JScrollPane(table);
        del.setEnabled(false);
        pan.add(pen);
        pan.add(numeField);
        pan.add(del);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                del.setEnabled(true);
                numeField.setText(model.getValueAt(table.getSelectedRow(), 1).toString());
            }
        });
        del.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                model.removeRow(table.getSelectedRow());
            }
        });
        Object[] row = {"1","NAME"};
        model.addRow(row);
        add(pan);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args) throws IOException {
        new Test();
    }
}

1 个答案:

答案 0 :(得分:2)

问题不在ActionListener,问题出在ListSelectionListener;删除行时,您删除的行不再存在;如果要更改选定的行,则必须使用事件中新选择的行;可能是使用getFirstIndex()

您还需要确保该行存在,以便在删除行时,仍然有一个有效的值可供选择。

换句话说,将ListSelectionListener更改为:

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent e){
          int firstIndex = e.getFirstIndex();
          if(firstIndex >= 0 && firstIndex < model.getRowCount()) {
            del.setEnabled(true);
            numeField.setText(model.getValueAt(firstIndex, 1).toString());
          } else {
            del.setEnabled(false);
          }
        }
    });