ArrayIndexOutOfBoundsException bei MyTableColumnModel

Status
Nicht offen für weitere Antworten.

Angel4585

Bekanntes Mitglied
Hallo,

in meinem eigenen TableColumnModel tritt der Fehler auf:
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
at models.MyTableColumnModel.getColumn(MyTableColumnModel.java:32)
at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderHeight(BasicTableHeaderUI.java:688)
at javax.swing.plaf.basic.BasicTableHeaderUI.createHeaderSize(BasicTableHeaderUI.java:721)
at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java:752)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1632)
at javax.swing.ViewportLayout.preferredLayoutSize(ViewportLayout.java:78)
at java.awt.Container.preferredSize(Container.java:1616)
at java.awt.Container.getPreferredSize(Container.java:1601)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1634)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:702)
at java.awt.Container.layout(Container.java:1432)
at java.awt.Container.doLayout(Container.java:1421)
at java.awt.Container.validateTree(Container.java:1519)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validate(Container.java:1491)
at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1421)
at javax.swing.plaf.basic.BasicTabbedPaneUI.tabForCoordinate(BasicTabbedPaneUI.java:1462)
at javax.swing.plaf.basic.BasicTabbedPaneUI.tabForCoordinate(BasicTabbedPaneUI.java:1456)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(BasicTabbedPaneUI.java:3599)
at java.awt.Component.processMouseEvent(Component.java:6035)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3983)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


Hier der Code:

Code:
/*
 * MyTableColumnModel.java
 *
 * Created on 30. November 2007, 14:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package models;

import java.util.Vector;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author M.Weber
 */
public class MyTableColumnModel extends DefaultTableColumnModel{
    
    /** Creates a new instance of MyTableColumnModel */
    public MyTableColumnModel() {
        super();
    }
    
    public TableColumn getColumn(int columnIndex) {
        TableColumn tc = null;
        try{          
            tc = super.getColumn(columnIndex);//Beim Aufruf von super.getColumn() tritt der Fehler auf
            switch(columnIndex){
                case 0:
                    tc.setWidth(80);
                    tc.setPreferredWidth(80);
                    tc.setResizable(false);
                    break;                
                case 1:
                    tc.setMinWidth(300);
                    tc.setPreferredWidth(300);
                    tc.setResizable(false);
                    break;                
                case 2:
                    tc.setMinWidth(200);
                    tc.setPreferredWidth(200);
                    tc.setResizable(true);
                    break;                
            }
            return tc;
        }catch(ArrayIndexOutOfBoundsException ex){
            ex.printStackTrace();
            return null;
            //ex.printStackTrace();
        }
    }

    public int getColumnCount() {
        return 3;
    }
}

Ausserdeme erzeuge ich das Ganze und weise es dem JTable zu:

Code:
tblTable.setModel(new MyTableModel());
tblTable.setColumnModel(new MyTableColumnModel());

Kann mir jemand sagen was das für ein Fehler ist? Ich mein ich tu ja nix besonderes eigentlich *grübel*
 

Marco13

Top Contributor
Der Fehler ist (höchstwahrscheinlich) ganz einfach der, dass das DefaultTableColumnModel keine TableColumns hat, d.h. dass getColumn(0) aufgerufen wird, ohne dass vorher (mindestens einmal) model.addColumn(eineTableColumn) aufgerufen wurde...
 

Angel4585

Bekanntes Mitglied
Ich dachte das übernimmt die Columns die das JTable schon hat o_O

Auf ejdenfall, mit folgender Ergänzung im Code gehts:

Code:
    public MyTableColumnModel(JTable aTable) {
        super();
        for(int i = 0 ; i < aTable.getColumnCount(); i++)
            addColumn(aTable.getColumnModel().getColumn(i));
    }
 
Status
Nicht offen für weitere Antworten.

Oben