Java Swing:Jtable ArrayIndexOutOfBoundsException

前端之家收集整理的这篇文章主要介绍了Java Swing:Jtable ArrayIndexOutOfBoundsException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有时会得到这个例外.我真的不知道哪里有问题,我使用了abstracttablemodel的实现.我无法找到发生异常的精确位置,因为当我使用调试器并逐步运行异常时不会发生.
Exception occurred during event dispatching:
    java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
    at javax.swing.plaf.basic.BasicTableUI.paintGrid(BasicTableUI.java:1931)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1767)
    at com.alee.laf.table.WebTableUI.paint(WebTableUI.java:52)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
    at javax.swing.JComponent.paintComponent(JComponent.java:752)
    at javax.swing.JComponent.paint(JComponent.java:1029)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JViewport.paint(JViewport.java:747)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1225)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:786)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1636)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
    at java.awt.Dialog$1.run(Dialog.java:1046)
    at java.awt.Dialog$3.run(Dialog.java:1098)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Dialog.show(Dialog.java:1096)
    at java.awt.Component.show(Component.java:1584)
    at java.awt.Component.setVisible(Component.java:1536)
    at java.awt.Window.setVisible(Window.java:842)
    at java.awt.Dialog.setVisible(Dialog.java:986)
    at ui.MainFrame.queryButtonActionPerformed(MainFrame.java:936)
    at ui.MainFrame.access$700(MainFrame.java:89)
    at ui.MainFrame$9.actionPerformed(MainFrame.java:538)
    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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.java:6288)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我的桌子型号:

/*
 * To change this template,choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entity3.Document;
import entity3.Msg;
import entity3.Senderreceiver;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author work
 */
public class IncomingMailTableModel extends AbstractTableModel{
    public String[] getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(String[] columnNames) {
        this.columnNames = columnNames;
    }
    private String[] columnNames = {"Дата","Тема","Відправник","Статус"};

    private List<Document> documents;

    public IncomingMailTableModel(List<Document> documents) {
        this.documents = documents;
    }

    @Override
    public int getRowCount() {
        return documents.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Document getObjectAtRow(int row) {     
        return documents.get(row);  
    }
    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case 0:
                return String.class;
            case 1:
                return String.class; 
            case 2:
                return String.class;
            case 3:
                return String.class;

        }
        return null;
    }

    @Override
    public String getValueAt(int row,int column) {
        String toReturn = "";
        Document document = documents.get(row);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Msg msg =document.getMsg();
        ArrayList<Senderreceiver> senderReceivers = new ArrayList<Senderreceiver>(msg.getSenderreceivers());
        //ArrayList<Document> docs = new ArrayList<Document>(document.getMsg().getDocuments());
        switch (column) {
            case 0:
                if (document.getMsg().getReceivingDate() != null) 
                    toReturn = format.format(document.getMsg().getReceivingDate());

                break;
            case 1:
                toReturn = document.getTitle();

                break;

            case 2:
                for (Object o : senderReceivers) {
                    Senderreceiver senderReceiver = (Senderreceiver) o;
                    if (senderReceiver.getRole()==0) {
                        toReturn = senderReceiver.getOrganization().getShortName();
                    }
                }
                break;
            case 3:
                if (document.getMsg().getIsRead()!=null)
                if(document.getMsg().getIsRead()) toReturn = "Прочитано";
                else toReturn="Непрочитано";
                break;
        }
        return toReturn;
    }
}

解决方法

如果要从Event Dispatch线程以外的线程修改TableModel(或TableModel“所在的数据结构”),则可能会发生这种情况.

错误的另一个常见原因是混合了视图和模型索引.例如,如果使用getSelectedRow()从JTable检索选定的行,则返回的值是基于视图的索引.如果您希望索引到相应的TableModel,则需要使用convertRowIndexToModel转换此值.

对于后一种情况,您可以尝试通过在不同列上对表进行排序,然后单击各行(特别是第一个和最后一个),或者在UI中执行“处理”该行所需的任何操作来复制此异常.

猜你在找的Java相关文章