I can't get my JTable to show anything

落花浮王杯 提交于 2019-11-29 16:48:23

only about mistakes in your code

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)

  • change info.add(button, BorderLayout.CENTER); to the NORTH or SOUTH

  • you not added JTable (in JScrollPane) to the JPanel correctly

pseudo code

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);
  • but this nothing above to solve something, because your issue should be some exception came from JDBC

    1. don't to create JComponents inside try - catch block, prepare this Object before, better could be as local variable

    2. don't to create XxxModel for JComponents inside try - catch block, prepare this Object before, better could be as local variable

    3. intialize XxxModel and its JComponent, then to load data from JDBC to the XxxModel

    4. add rs.close() to the finally block (try - catch - finally)

  • don't reinvent the wheel, use

    1. ResultSetTableModel

    2. Table From Database by @camickr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!