Adam Outler
Adam Outler

Reputation: 1671

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?

Upvotes: 1

Views: 8234

Answers (2)

naikus
naikus

Reputation: 24472

use javax.swing.table.DefaultTableModel

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

Upvotes: 2

Guillaume
Guillaume

Reputation: 14656

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);

Upvotes: 2

Related Questions