如何使用JTable在Java中显示所有行

时间:2019-03-14 13:27:06

标签: java jtable

我想问一下如何使用JTable在Java中显示所有行。 我尝试在其中使用while条件是要评估的result.next()。

我尝试使用System.out.println打印它,它显示所有记录,但是当我在JTable上尝试它时,它仅显示最后一行。

`

while(results.next()) {

    String id = results.getString("id");
    String date_collected = results.getString("date_collected");
    String date_disposed = results.getString("date_disposed");
    String person_in_charged = results.getString("person_in_charged");
    String status = results.getString("status");

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
       new Object [][]){
            {id, date_collected, date_disposed, person_in_charged, status}
       },
       new String [] {
             "ID", "Date collected", "Date disposed", "person in charged", 
             "status"
       }
     )); 

     jScrollPane1.setViewportView(jTable1);

     if (jTable1.getColumnModel().getColumnCount() > 0) {
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);
      }
 }

`

能帮我谢谢吗。

2 个答案:

答案 0 :(得分:0)

尝试累积数据,如下所示:

// to hold the data
List<Object[]> data = new ArrayList<>();

// loop through the ResultSet
while(results.next())
{
    String id = results.getString("id");
    String date_collected = results.getString("date_collected");
    String date_disposed = results.getString("date_disposed");
    String person_in_charged = results.getString("person_in_charged");
    String status = results.getString("status");

    // add a new row of data
    data.add(new Object[] {id, date_collected, date_disposed, person_in_charged, status});
}

// after the loop: set the data as the model
jTable1.setModel(new javax.swing.table.DefaultTableModel(
    // convert from List to array
    data.toArray(new Object[][data.size()],
    // column headers
    new String [] { "ID", "Date collected", "Date disposed", "person in charged", "status" }
)); 

// other details from your code: moved outside of the loop...
jScrollPane1.setViewportView(jTable1);

if (jTable1.getColumnModel().getColumnCount() > 0)
{
    jTable1.getColumnModel().getColumn(0).setResizable(false);
    jTable1.getColumnModel().getColumn(1).setResizable(false);
    jTable1.getColumnModel().getColumn(2).setResizable(false);
    jTable1.getColumnModel().getColumn(3).setResizable(false);
 }

答案 1 :(得分:0)

DefaultTableModel应该在循环之外创建和设置。

    DefaultTableModel model = new DefaultTableModel();

    model.addColumn("you column names");
    model.addColumn("another column name");
    //you can add all your column headers here      

    JTable table = new JTable();

    table.setModel(model);

   while(rs.next())
    {
         String id = results.getString("id");
         String date_collected = results.getString("date_collected");
         String date_disposed = results.getString("date_disposed");
         String person_in_charged = results.getString("person_in_charged");
         String status = results.getString("status");


         model.addRow(new Object[]{id, date_collected, date_disposed, person_in_charge, status });
    }

,然后在此处继续其余代码。