Java TableCellListener导致数组索引超出绑定错误

时间:2014-02-18 17:26:56

标签: java jtable

我编写了一个使用TableCellListener类的程序。

代码编译正确但是当我运行程序时,当我使用 Tab 键从一个单元格导航到单元格时,我得到以下错误:

Exception in thread "AWT-EventQueue-0" 
                           java.lang.ArrayIndexOutOfBoundsException: -1

仅当我使用 Tab 键时才会发生这种情况。如果我使用鼠标,该程序可以正常工作。另一个用户在他的计算机上运行完全相同的程序,但无法复制问题。一切都运行得很好。他正在使用Windows,但我正在使用Mac。有没有其他人在TableCellListener遇到此问题?

以下是代码:

package testyo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import java.text.*;


public class Testyo {
JFrame mainPanel;
RXTable myTable;
DefaultTableModel myTableModel;

Testyo() {
    mainPanel = new JFrame();
    mainPanel.getContentPane().setLayout(new FlowLayout());
    mainPanel.setSize(1200, 1200);
    mainPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] columnNames = {"ID", "First Name", "Last Name", "Salary"};
    Object[][] data = {
        {1, "aaa", "bbb", 2000}, 
        {2, "ccc", "ddd", 2300},
        {3, "eee", "fff", 1900}
    };
    myTable = new RXTable(new MyTableModel(data, columnNames));
    myTable.setCellSelectionEnabled(true);
    myTable.setSelectAllForEdit(true);
    JScrollPane myScrollPane = new JScrollPane(myTable);
    myTable.setPreferredScrollableViewportSize(new Dimension(1000, 400));
    mainPanel.getContentPane().add(myScrollPane);
    mainPanel.setVisible(true);


    Action action = new AbstractAction () {
        public void actionPerformed(ActionEvent e) {
            TableCellListener tcl = (TableCellListener)e.getSource();
            System.out.println("***");                         
        }
    };
    TableCellListener tcl = new TableCellListener(myTable, action);
}


class MyTableModel extends DefaultTableModel {

    public MyTableModel(Object[][] data, String[] columnNames) {
        super(data, columnNames);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return !(col==0);
    }

    @Override
    public Class getColumnClass(int col) {
        if (col == 0 | col == 3)
            return Integer.class;
        else
            return String.class;
    }
}

public static void main(String[] args) {
    // TODO code application logic here
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           new Testyo();
       } 
    });
}
}

这是完整的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at testyo.TableCellListener.run(TableCellListener.java:145)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at testyo.TableCellListener.processEditingStopped(TableCellListener.java:154)
at testyo.TableCellListener.propertyChange(TableCellListener.java:121)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.Component.firePropertyChange(Component.java:8382)
at javax.swing.JTable.setCellEditor(JTable.java:5633)
at javax.swing.JTable.removeEditor(JTable.java:5822)
at javax.swing.JTable.editingStopped(JTable.java:4723)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:233)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5477)
at javax.swing.plaf.basic.BasicTableUI$Actions.actionPerformed(BasicTableUI.java:502)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JTable.processKeyBinding(JTable.java:5258)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2940)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

0 个答案:

没有答案