我无法让我的 gui 显示 Jtable,为什么我不知道并且我没有收到任何错误,当我在屏幕上打印某些内容时,我得到 9 列。所以我得到数据。但我做错了什么我对此一无所知。
我的 GUIOdreHandler 看起来像这样
public GUIOrdreHandler(){
KaldSQL ks = new KaldSQL();
ResultSet rs;
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
java.sql.ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
System.out.println(columnCount);
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
我的 GUIHentOrdre 看起来像这样
public GUIHentOrdre(){
try {
con = ks.connectNow();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
ResultSet rs = ks.Hentalleordreliste(con);
GUIOrdreHandler gh = new GUIOrdreHandler();
try {
table = new JTable(gh.buildTableModel(rs));
System.out.println(table);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info.add(table, BorderLayout.CENTER);
add(table);
}
}
我已经尝试过任何谷歌,书北工程,所以请帮助我:D