2011-12-02 10 views
5

I 時には例外が発生します。私は本当にどこに問題があるのか​​わかりません、私はabstracttablemodelの私の実装を使用します。私はCANTがどこに例外が発生excact場所を見つけた、私はデバッガを使用してbeacauseを実行し、ステップバイステップ例外が発生しません。Java Swing:Jtable ArrayIndexOutOfBoundsException

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; 
    } 
} 
+2

この例外が発生する場所でコードを入力できますか? – gprathour

+1

コードを表示してください! – Jonas

+1

別のスレッドからテーブルの列を追加/削除していますか? –

答えて

11

これは、イベントディスパッチスレッド以外のスレッドからTableModel(またはTableModelが置かれているデータ構造)を変更している場合に発生します。

このエラーのもう1つの一般的な原因は、ビューとモデルのインデックスを混在させることです。たとえば、getSelectedRow()を使用してJTableから選択した行を取得する場合、返される値はビューベースのインデックスです。対応するTableModelにインデックスを作成する場合は、convertRowIndexToModelを使用してこの値を変換する必要があります。

後者の場合、異なる列でテーブルをソートし、さまざまな行(特に最初と最後)をクリックするか、UIで必要な処理を実行してこの処理を「処理」することで、その行

+0

にお返事ありがとうございましたListのsize()を返信します。私はテーブルを並べ替えていないので、モデルとビューのインデックスは混乱することはありません。だから私はその最初のケースだと思う。どうすればこの問題を解決できますか? 'SwingUtilities.invokeLater'を呼び出すことによって?前もって感謝します。 – bunnyjesse112

+1

そうです。モデル/ UIを更新する場所ではいつでもSwingUtilities.isEventDispatchThread()をアサートできます。 – Adamski

+0

私はこの問題を取り除いたと思う。助けてくれてありがとう、Adamski!あなたは揺れる! – bunnyjesse112

3

あなたpublic int getRowCount()戻ってあなたのベクトルのsize()ていることを確認します。

+0

回答ありがとうございます。テーブルモデル – bunnyjesse112

関連する問題