Putting an array of objects into a table - Java

I have a student object with the following attributes:

private String forename;
private String surname;
private String id;
private char gender;
private String street;
private String locality;
private String postcode;
private String email;

I have several of these objects stored in an ArrayList and wanted to know the best way to get them into a JTable with columns named after the attributes above.

Upvotes: 1

Views: 77

Answers (1)

Prakhar Nigam
Prakhar Nigam

Reputation: 658

You can use do it simply like this. or check this answer as well.Load arrayList data into JTable

  public void populateReadersTable(ReaderList readerList) {
        DefaultTableModel model = (DefaultTableModel) jTableReaders.getModel();
        model.setRowCount(0);
        model.setColumnCount(0);
        model.addColumn("ID");
        model.addColumn("First Name");
        model.addColumn("Last Name");
        model.addColumn("Email");
        model.addColumn("Mobile");
        model.addColumn("Street");
        model.addColumn("City");
        model.addColumn("Postal Code");
        model.addColumn("National ID");

        for (Reader reader : readerList) {
            model.addRow(reader.toStringArray());
        }

    }

Upvotes: 3

Related Questions