1

I use DefaultTableModel for my JTable model, But not show my table!

public class RecordTableGUI2 extends JFrame {

JTable table;
RecordTableModel2 model2;

public RecordTableGUI2() {
    model2 = new RecordTableModel2();
    table = new JTable(model2);

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

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new RecordTableGUI2();
        }
    });
}
}

Model Class:

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


public RecordTableModel2() {
    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)); //prints correct
        }

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

How to introduce vectors to DefaultTableModel?

Output just show a blank JFrame

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • This is getting annoying. I gave you code 2 weeks ago on how to load data from a database into a DefaultTableModel. Why is it so hard for you to listen to the suggestion? Where do you actually add the data and column names to the DefaultTableModel? The DefaultTableModel doesn't know about your data/column Vectors. Again look at the code I gave you. Start with working code and then make changes if it doesn't do exactly what you want!!! – camickr Aug 17 '13 at 22:08
  • @camickr Yes, I don't know how introduce my vectors to `DefaultTableModel`? – Sajad Aug 17 '13 at 22:12
  • Edxactly, you create the Vectors, then you create the DefaultTableModel using the Vectors. I showed you how to create the Vectors in the other posting. Then you create the JTable using the model. – camickr Aug 17 '13 at 22:15
  • @camickr Well, Why should not extend DefaultTableModel? And when should extend DefaultTableModel? – Sajad Aug 17 '13 at 22:17
  • I suggested already you need to extend the model when you need to add functionality. You don't need to add functionality to load data into the model. – camickr Aug 17 '13 at 22:18
  • @camickr I will need to add some functionality in future, like add/Remove/ edit... – Sajad Aug 17 '13 at 22:20
  • @camickr So now should i extend or not? – Sajad Aug 17 '13 at 22:22
  • See also this [answer](http://stackoverflow.com/a/15002016/230513). – trashgod Aug 18 '13 at 00:52

1 Answers1

0

Add this at first statement of constructor:

super(data,column);

And declare data and column as static

Sajad
  • 2,273
  • 11
  • 49
  • 92