Java 我能';我不能让我的孩子们展示任何东西
我不能让我的gui显示Jtable,为什么我不知道,我没有得到任何错误,当我在屏幕上打印一些东西时,我得到9列。所以我得到了数据。但我做错了什么我不知道 我的GUIOdreHandler看起来像这样Java 我能';我不能让我的孩子们展示任何东西,java,swing,jtable,jpanel,jscrollpane,Java,Swing,Jtable,Jpanel,Jscrollpane,我不能让我的gui显示Jtable,为什么我不知道,我没有得到任何错误,当我在屏幕上打印一些东西时,我得到9列。所以我得到了数据。但我做错了什么我不知道 我的GUIOdreHandler看起来像这样 public GUIOrdreHandler(){ KaldSQL ks = new KaldSQL(); ResultSet rs; } public static DefaultTableModel build
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);
}
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
只是关于代码中的错误
JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
- 删除关于
add的代码行(按钮)代码>到(不完全是关于代码的)
- 更改
info.add(按钮,BorderLayout.CENTER)编码>到
或北部
南部
- 您没有将
(在JTable
中)正确添加到JScrollPane
JPanel
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
- 不要在
-try
块中创建catch
s,在之前准备这个JComponent
,最好是作为对象
局部变量
- 不要在
中为
s创建JComponent
,XxxModel
-try
块,在之前准备这个catch
,最好是作为对象
局部变量
- 初始化
及其XxxModel
,然后将数据从JComponent
加载到JDBC
XxxModel
- 将
添加到rs.close()
块(finally
-try
-catch
)finally
- 不要重新发明轮子,使用它
JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
- 删除关于
add的代码行(按钮)代码>到(不完全是关于代码的)
- 更改
info.add(按钮,BorderLayout.CENTER)编码>到
或北部
南部
- 您没有将
(在JTable
中)正确添加到JScrollPane
JPanel
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
- 不要在
-try
块中创建catch
s,在之前准备这个JComponent
,最好是作为对象
局部变量
- 不要在
中为
s创建JComponent
,XxxModel
-try
块,在之前准备这个catch
,最好是作为对象
局部变量
- 初始化
及其XxxModel
,然后将数据从JComponent
加载到JDBC
XxxModel
- 将
添加到rs.close()
块(finally
-try
-catch
)finally
- 不要重新发明轮子,使用它