-2

I have two class:

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();
        }
    }
}

My AddRecord Class:

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();
        }
    }
}

My RecordTableModel2 Class:

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();
            }
        }
    }
}

When i click to showGui2Button , Then this exception occur

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)
nIcE cOw
  • 23,893
  • 7
  • 41
  • 127
Sajad
  • 2,016
  • 10
  • 44
  • 81
  • 5
    The problem is in the code you're not showing us - provide an `SSCCE` – Reimeus Aug 25 '13 at 21:28
  • Paste the **complete** stack trace. And show us the code of the method of yours that appears first in the stack trace. – JB Nizet Aug 25 '13 at 21:29
  • I think problem is for using of `initComponents();` method that is `IDE` generated codes – Sajad Aug 25 '13 at 21:30
  • @JBNizet i put complete stack trace. – Sajad Aug 25 '13 at 21:33
  • OK. So the exception is caused by a table model. Show us the code creating and populating this table model. – JB Nizet Aug 25 '13 at 21:35
  • Wild guess: your table model returns a size bigger than its actual size. Check if you implemented properly `getRowCount` or `getColumnCount` – Guillaume Polet Aug 25 '13 at 21:38
  • @GuillaumePolet I add table model class, I use `DefaultTableModel` – Sajad Aug 25 '13 at 21:39
  • 1
    -1, This is the 5th or 6th (I"ve lost count) question on this topic. The OP still posts code related to the database which he has been told is of no use to us because we don't have access to the database. The OP still refuses to post a SSSCE. To OP has been told there is no reason to extend the DefaultTableModel top populate it with data and has been given a link to working code from my blog. Yet the OP is still wasting peoples time. – camickr Aug 25 '13 at 22:32
  • @camickr I remember that you told me if i want to add more methods, Like `add` or `edit` or... , I should extend from DefaultTableModel . And now you say that there is no reason to extend the DefaultTableModel . Why? – Sajad Aug 26 '13 at 16:45
  • I said you don't need to extend the DefaultTableModel to load data into the model. I also said the DefaultTableModel supports methods like add/remove. – camickr Aug 26 '13 at 18:00
  • Nice, So what is your mean about `don't need to extend the DefaultTableModel to load data into the model` ? So how load data into model without `DefaultTableModel` ? – Sajad Aug 26 '13 at 18:52
  • I can load data into Vectors and then put in `DefaultTableModel` constructor, For example. – Sajad Aug 26 '13 at 18:57

1 Answers1

3

All the code used to initialize the table model shouldn't be part of its constructor. Don't create a class extending DefaultTableModel, and don't use static variables for the data and columns: you don't want all the instances of your model to share a unique data vecstor and a unique columns vector.

Move all this code to a simple method which, once the data and columns names are ready, creates the model:

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);
}
JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174