JTable在绘制期间抛出IndexOutOfBoundsException

时间:2012-06-05 13:17:55

标签: java swing jtable defaulttablemodel indexoutofboundsexception

您好我正在使用JTable构建应用程序来显示数据库中的数据,并且在绘画过程中我不断从IndexOutOfBoundException获得tableModel.getValueAt(row,col)。我调试了代码,似乎数据没有保存到DefaultTableModels数据向量。

这是我的代码......

    public class TableFrame extends JFrame{
        public static void main(String[] args){
            TableFrame frame = new TableFrame();

        }

        private TablePanel tablePanel;
        public TableFrame(){
            super();
            setSize(new Dimension(500,500));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            tablePanel = new TablePanel();
            add(tablePanel);

            updateLabels();
            setVisible(true);
        }

        public void updateLabels() {
            setTitle("Main Frame");

            tablePanel.updateLabels();
        }

        private static class TablePanel extends JPanel{

            private DefaultTableModel tableModel;
            private DefaultTableColumnModel colModel;
            private JTable table;

            private JPanel buttonPanel;
            private JButton refreshButton;

            public TablePanel(){
                setLayout(new BorderLayout());

        buttonPanel = new JPanel(new FlowLayout());
        refreshButton = new JButton();
        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                refreshData();
            }
        });
        buttonPanel.add(refreshButton);
        add(buttonPanel,BorderLayout.NORTH);

        //TODO:Set the column renderers and editors
        colModel = new DefaultTableColumnModel();
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));
        colModel.addColumn(new TableColumn(0,75));

        tableModel = new DefaultTableModel();

        table = new JTable(tableModel,colModel);

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane,BorderLayout.CENTER);

        updateLabels();
    }

    //This is going to query data from a database and populate the table
    //problem occurs during painting
    public void refreshData(){
        Random rand = new Random();

        for(int i=0;i<1000;i++){
            Vector<Object> fields = new Vector<Object>();
            for(int j=1;j<=11;j++){
                fields.add(rand.nextInt());
            }
            tableModel.addRow(fields);
        }
    }

    public void updateLabels() {
        colModel.getColumn(0).setHeaderValue("Col 1");
        colModel.getColumn(1).setHeaderValue("Col 2");
        colModel.getColumn(2).setHeaderValue("Col 3");
        colModel.getColumn(3).setHeaderValue("Col 4");
        colModel.getColumn(4).setHeaderValue("Col 5");
        colModel.getColumn(5).setHeaderValue("Col 6");
        colModel.getColumn(6).setHeaderValue("Col 7");
        colModel.getColumn(7).setHeaderValue("Col 8");
        colModel.getColumn(8).setHeaderValue("Col 9");
        colModel.getColumn(9).setHeaderValue("Col 10");
        colModel.getColumn(10).setHeaderValue("Col 11");

                refreshButton.setText("Refresh");

                repaint();
            }
        }
    }

这是例外......

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:447)
at TableFrame$TablePanel.refreshData(TableFrame.java:108)
at TableFrame$TablePanel$1.actionPerformed(TableFrame.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6268)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6033)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4629)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4455)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
at java.awt.Container.dispatchEventImpl(Container.java:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2517)
at java.awt.Component.dispatchEvent(Component.java:4455)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

我调试了代码并添加了对tableModel.getDataVector().getElementAt(0).getElementAt(0)tableModel.getRowCount()的调用。 getRowCount()返回1000getElementAt()导致IndexOutOfBounds异常,就像在awt事件队列中一样。我认为我所做的应该有效,但显然有些东西我不知道。

1 个答案:

答案 0 :(得分:1)

TableModel未引用ColumnModel来获取列数,因此必须使用tableModel.setColumnCount()进行设置。否则,defaultTableModel会对数据进行验证并将行截断为getColumnCount()后面的数字。