I have a Java Swing dialog that utilizes JXTreeTable
. I need to be able to add content to the JXTreeTable
periodically (say, once a minute). How do I access that JXTreeTable
from another class?
I'm so used to ExtendScript/JavaScript that I want to say something like frame.JXTreeTable.contents = x
to set the contents of that TreeTable
. Of course, it's nowhere near this easy in Java. How can I accomplish this?
You have any number of options available to you, depending on what it is you want to achieve...
You Could...
Pass the
TreeModel
to the classes responsible for performing the updates.This is a little troubling as it provides access to the
TreeModel
that you may not want to provide to other classes, these classes can suddenly do things to the model that you may not want them to, such as change the root not, remove nodes, add nodes to places you don't want them added...It also assumes a common knowledge of the tree structure. For example, you may only want the updates to occur within a certain sub tree, this now requires the update classes to know this implicitly.
It could also lock you into a given
TreeModel
which you may not want in the future, especially if you'd like to re-use the update code...You Could...
Use an observer pattern or even a producer/consumer pattern within the update classes.
Basically this means that the update classes just "do stuff" and trigger events to notify anybody who might be interested that changes have occurred. You see this concept A LOT within Swing.
The benefit of this is you decouple the update portion of you code from the model and the UI, making the code more flexible and reducing assumptions about other parts of the code.
It would then be up to the observer/listener to make decisions about how to respond to these updates and changes, making the code infinitely more flexible.
It means that you can change the update code (and so long as the observer interface doesn't change) it won't effect those who are interested in the out come of the results...