Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Extend AbstractTableModel and make the enclosed data structure generic in whatever way suits your use-case. This example wraps a Map<String, String>, but you can also use Class Literals as Runtime-Type Tokens, as is done in DbUtils and BeanTableModel, cited here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top