Why am I getting an ArrayIndexOutOfBoundsException when I delete JTable row?

廉价感情. 提交于 2019-12-24 00:28:32

问题


I am getting .ArrayIndexOutOfBoundsException when i try to delete a row from my JTable.

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)

First, I select the row I want to delete and then when I fire the action the delete button it freezes a bit and gives me exception.

Here is my complete code:

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();
    }
}

Full stack trace :

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementData(Unknown Source)
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
    at Test$1.valueChanged(Test.java:34)
    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.removeIndexInterval(Unknown Source)
    at javax.swing.JTable.tableRowsDeleted(Unknown Source)
    at javax.swing.JTable.tableChanged(Unknown Source)
    at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
    at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(Unknown Source)
    at javax.swing.table.DefaultTableModel.removeRow(Unknown Source)
    at Test$2.actionPerformed(Test.java:39)
    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)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

回答1:


The problem is not in the ActionListener, the problem is in the ListSelectionListener; when you delete the row, the row that you're deleting no longer exists; if you want to change the selected row, you have to use the new selected row from the event; probably by using getFirstIndex().

You also need to ensure that the row exists, so that when you delete a row, there's still a valid value to select.

In other words, change your ListSelectionListener to this:

    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);
          }
        }
    });


来源:https://stackoverflow.com/questions/32612014/why-am-i-getting-an-arrayindexoutofboundsexception-when-i-delete-jtable-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!