-2

我有两个班级:

public class RecordTableGUI2 extends JFrame implements ActionListener {

    private JButton addButton;
    JTable table;
    RecordTableModel2 model2;

    public RecordTableGUI2() {

        model2 = new RecordTableModel2();
        table = new JTable(model2);
        ...
        add(buttonPanel(), BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700, 550);
        setLocation(300, 80);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addButton) {
            this.setVisible(false);
            new AddRecord();
        }
    }
}

我的AddRecord班级:

public class AddRecord extends javax.swing.JFrame implements ActionListener {

    private JButton showGui2Button;

    public AddRecord() {
        initComponents();
        showGui2Button.addActionListener(this);
        setLocation(300, 100);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == showGui2Button) {
            this.setVisible(false);
            new RecordTableGUI2();
        }
    }
}

我的 RecordTableModel2 类:

public class RecordTableModel2 extends DefaultTableModel {

    static Vector data = new Vector();
    static Vector column = new Vector();
    Connection con;
    Statement statement;
    ResultSet result;
    String dbUrl = "jdbc:mysql://localhost/mydb";
    String query = "Select * from mytable";

    public RecordTableModel2() {
        super(data, column);
        try {
            con = DriverManager.getConnection(dbUrl, "root", "2323");
            statement = con.createStatement();
            result = statement.executeQuery(query);

            int c = result.getMetaData().getColumnCount();
            for (int i = 1; i <= c; i++) {
                column.add(result.getMetaData().getColumnName(i));
                System.out.println(result.getMetaData().getColumnName(i));
            }

            while (result.next()) {
                Vector eachRow = new Vector(c);
                for (int i = 1; i <= c; i++) {
                    eachRow.add(result.getString(i));
                    System.out.println(result.getString(i));
                }
                data.add(eachRow);
            }           
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException sqlee) {
                sqlee.printStackTrace();
            }
        }
    }
}

当我单击 showGui2Button 时,会发生此异常

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:650)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
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)
4

1 回答 1

3

所有用于初始化表模型的代码都不应该是其构造函数的一部分。不要创建扩展 DefaultTableModel 的类,也不要对数据和列使用静态变量:您不希望模型的所有实例共享唯一的数据向量和唯一的列向量。

将所有这些代码移到一个简单的方法中,一旦数据和列名准备好,就创建模型:

private TableModel createTableModel() {
    Vector data = new Vector();
    Vector columns = new Vector();

    // populate data and columns using JDBC. Use only local variables

   return new DefaultTableModel(data, columns);
}
于 2013-08-25T21:52:46.843 回答