1

DefaultTableModel用于我的JTable模型,但不显示我的表!

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

型号类:

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

如何将向量引入DefaultTableModel

输出只显示一个空白的 JFrame

4

1 回答 1

0

在构造函数的第一个语句中添加:

super(data,column);

data并将and声明column为静态

于 2013-08-18T08:10:45.800 回答