create TableModel and populate jTable dynamically

前端 未结 3 1734
遥遥无期
遥遥无期 2021-01-12 10:54

I want to store the results of reading lucene index into jTable, so that I can make it sortable by different columns. From index I am reading terms with different measures o

3条回答
  •  北海茫月
    2021-01-12 11:45

    There is no need to create a custom TableModel for this. Just use the DefaultListModel.

    The DefaultListModel allows you to dynamcially add rows to the model using the addRow(...) method and it automatically invokes the appropriate fireXXX method to tell the table to repaint itself.

    The basic code would be:

    DefaultTableModel model = new DefaultTableModel( columnNames );
    
    while (...)
    {
      Vector row = new Vector();
      row.add(...)
      row.add(...)
      model.addRow( row );
    }
    
    JTable table = new JTable( model );
    

    You don't need to create a custom TableModel everytime. Sometimes you can start with the DefaultTableModel.

提交回复
热议问题