Fluffy
Fluffy

Reputation: 28362

How can I bind an object to a row in a Swing table?

In my application I have some similar threads doing their stuff and I'd like to represent some of their properties in a row of a table (one thread per row). I'd pass a row object to corresponding thread and update them on changing values, but I couldn't find anything like that in API. So what is the right strategy to keep rows updated with actual properties?

Upvotes: 1

Views: 1170

Answers (2)

ZeissS
ZeissS

Reputation: 12135

Take a look at binding apis, e.g. the JGoodies binding api.

Upvotes: 0

akf
akf

Reputation: 39485

Using a JTable, the traditional way to create such a table would be to have a class implement TableModel (or extend AbstractTableModel or DefaultTableModel). There you would maintain your data, and do so in such a way that it is easy for you to look up a data structure per Thread. You would implement the getValueAt method to return the values for your Threads per row. to When a Thread's properties change, it would then go an update the TableModel. Then you would need to tell your JTable that your data has changed and have it update from the model. You would do this by firing a tableChanged event. AbstractTableModel and its descendants have variety of fireTableChanged-type methods available. Make sure you do this firing of events in the EventDispatchThread. See topics on currency in Swing and the SwingWorker for info on worker threads interacting with painting GUIs.

Upvotes: 4

Related Questions