Changing a Swing control dynamically

107 views Asked by At

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?

3

There are 3 answers

1
MadProgrammer On

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...

1
Paprik On

You need to make your JXTreeTable a public variable inside the class that it's in (let's say AwesomeDialog). Then you could access it through the AwesomeDialog's instance with the dot notation e.g. dialog.table

Probably check out: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

0
DuncanKinnear On

Again, as with my answer to your other question, you need to think about where the code that modifies your table should be.

Ask yourself why the code to modify the table is not in the dialog itself?

For instance, if your dialog doesn't know about the database, then you need to pass a database connection into the dialog so that it can perform any database-related functions itself.

Again, think about your windows as rooms. You might be in the living room with a pile of papers, but the table you want to put those papers on is in the dining room. You don't try to put the papers on the table in the dining room from within the living room. Instead, you take the papers into the dining room and put them on the table there.

Similarly, if you want to display data in a table in a dialog, you either pass the data set to the dialog, or you pass a database connection to the dialog and it looks up the data to populate the table.