简体   繁体   中英

How to display a 2D array in jTable?

I have a static 2D array called Status.Data[][] and a Column header called Status.Columns[] .

I am using net beans and I want to be able to have the arrays populate the table.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jTable1.setColumnModel(new DefaultColumnModel(Status.Data, Status.Columns));
    }

This throws an error that it is expecting a TableColumnModel.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jTable1.setColumnModel(new TableColumnModel(Status.Data, Status.Columns));
    }

This says java.swing.table.TableColumnModel is abstract and cannot be instantiated.

I would even be happy if I could figure out how to make it display when the window is opened.

How do I populate my table?

You can create the table model and then pass it to the table constructor:

TableModel model = new DefaultTableModel(Status.Data, Status.Columns);
JTable table = new JTable(model);

使用javax.swing.table.DefaultTableModel

DefaultTableModel(Object[][] data, Object[] columnNames)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM