Generic way to give a table model a list of objects

Is there a way to make a generic table model that take a list of object and present them in the jtable.
i can make a transfer object that has an array of strings define table column and a list of object then use reflection on the object to get the fields data to be presented in the table, but is there a more efficient way?

Answer

then use reflection on the object to get the fields data to be presented in the table

This is the way the Bean Table Model works.

but is there a more efficient way?

Then you need to create a custom model. Check out the JButtonTableModel.java from the above link. It shows the minimal amount of code you would need to create a custom model.

Both of the above examples extend the functionality of the Row Table Model which is used to provide the generic Object storage and provide common model functions like add, remove.

Leave a Reply

Your email address will not be published. Required fields are marked *