Q:

How to populate a JTable with object attributes in Java?

Say I have a class 'Dog' for creating dog objects. It would look like so:
public class Dog {
private String name;
private String colour;
private String size;
private int age;

public Dog(String name, String colour, String size, int age){
    this.name = name;
    this.colour = colour;
    this.size = size;
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

public String getSize() {
    return size;
}

public void setSize(String size) {
    this.size = size;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
  }
}

Say now I have created and stored in an ArrayList 10 dog objects. How do I display these using a JTable with the column names being name, colour, size, etc?

A:

You can populate jtable  like below :
public void initTable(ArrayList<Dog> dogList, JTable table) {

  DefaultTableModel model = (DefaultTableModel)table.getModel();
     for(Dog dog : dogList){
          model.addRow(new Object[]{dog.getName(), dog.getColor(), dog.getAge(), ... });
     }
     table.setModel(model);
}

And you can implement your table model like below :
new javax.swing.table.DefaultTableModel (
    new String [] {
        "Name","Color", "Age", ...
    },0
) {
    Class[] types = new Class[]{
        java.lang.String.class,
        java.lang.String.class,
        java.lang.Integer.class, 
        ...
    };
}

