我不能让我的JTable显示任何东西 [英] I can't get my JTable to show anything
问题描述
我不能让我的gui显示Jtable,为什么我不知道,我没有得到任何错误,当我打印一些东西到屏幕上我得到9列。所以我得到了数据。但是我做错了我不知道。
I cant get my gui to show the Jtable, why i dont know and i dont get any error and when i print something to the screen i get 9 colum. so i get data. but what i'm doing wrong i have no idea about that.
我的GUIOdreHandler看起来像这样
My GUIOdreHandler looks like this
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看起来像这样
And my GUIHentOrdre looks like this
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
I have tried anything google, book northing works, so please help me :D
推荐答案
关于代码中的错误
JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
-
删除关于
的代码行添加(按钮);
到(代码不完全在谈论)remove code line about
add(button);
to the (code not exactly talking about)更改
info.add(按钮) ,BorderLayout.CENTER);
到NORTH
或SOUTH
您没有将
JTable
(在JScrollPane
中)添加到JPanel
正确you not added
JTable
(inJScrollPane
) to theJPanel
correctly伪代码
JPanel info = new JPanel(); info.setLayout(new BorderLayout()); button = new JButton("button"); info.add(button, BorderLayout.SOUTH); JTable table = new JTable (ClassOrVoidOrModelNameReturnsTableModel) JScrollPane scroll = new JScrollPane(table) info.add(scroll, BorderLayout.CENTER);
-
但上面没什么好解决的,因为你的问题应该是一些例外来自
JDBC
-
不要创建
JComponent
s里面尝试
-catch
阻止,准备这个对象
之前,更好的可能是本地变量
don't to create
JComponent
s insidetry
-catch
block, prepare thisObject
before, better could be aslocal variable
不要在
里面
-JComponent
创建XxxModel
试试catch
阻止,准备此对象
之前,更好的可能是本地变量
don't to create
XxxModel
forJComponent
s insidetry
-catch
block, prepare thisObject
before, better could be aslocal variable
初始化
XxxModel
及其JComponent
,然后将数据从JDBC
加载到XxxModel
intialize
XxxModel
and itsJComponent
, then to load data fromJDBC
to theXxxModel
将
rs.close()
添加到finally
块(尝试
-catch
-最后
)add
rs.close()
to thefinally
block (try
-catch
-finally
) -
-
不要重新发明轮子,请使用
don't reinvent the wheel, use
Table From Database by @camickr
-
这篇关于我不能让我的JTable显示任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!