The most straightforward way to create your own table model is to inherit from AbstractTableModel. Then override, at a minimum, getRowCount, getColumnCount, and getValueAt.
You can then either create the table using "new JTable(mymodel)", where "mymodel" is an instance of the model you have created, or you could create it with just "new JTable", and then later do setModel to attach your model to the JTable.
For example -- and while I've done this a bazillion times, I'm writing this off the top of my head so no warranties expressed or implied, odds are there's a syntax error or two in there somewhere:
class ArrayListModel extends AbstractTableModel
{
ArrayList<Object[]> list;
public ArrayListModel(ArrayList<Object[]> list)
{
this.list=list;
}
public int getRowCount()
{
return list.size();
}
public int getColumnCount()
{
if (list.size()==0)
{
return 0;
}
else
{
return list.get(0).length;
}
}
public Object getValueat(int row, int column)
{
return list.get(row)[column];
}
}
... somewhere else ...
ArrayListModel mymodel=new ArrayListModel(somedata);
JTable mytable=new JTable(mymodel);
... etc ...
Side note: When I was first learning Java, I thought that the "normal" way to create a JTable was to use the default model, create a vector of vectors or a 2-D array and then populate. I thought creating your own model would be something you did in rare, odd cases. But I've since figured out that the default model is only the best way in rare, simple cases. Now I almost always create my own model: it's usually easier to code and easier to understand. Typically I have an ArrayList of some class that I've created with specific fields, and then in my model class I have something like:
public Object getValueAt(int row, int col)
{
Whatever w=list.get(row);
if (col==0) return w.foo;
else if (col==1) return w.bar;
else if (col==2) return w.plugh;
else throw SomeException();
}
Or use a case statement. Same idea.