我将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

最佳答案

在构造函数的第一条语句中添加以下内容:

super(data,column);


并声明datacolumn为静态

关于java - 具有DefaultTableModel的简单JTable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18293693/

10-12 06:07