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 objec开发者_运维技巧t 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?
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.
Take a look at binding apis, e.g. the JGoodies binding api.
精彩评论