• Post Reply Bookmark Topic Watch Topic
  • New Topic

SWT: access to controls from outside main  RSS feed

 
Mariano Lopez-Gappa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is the first of many beginner questions, as I'm trying to get comfortable with a few Java frameworks, and the Java language (and Eclipse IDE as well) on my free time. First of all thanks in advance for wasting your precious time helping me.

Here it goes:
How do I access say a label control which I've already created in the main method of a class which creates a SWT display, shell...from outside of this class?

Given that I also need access to the Shell instance because I alter its title text, what I've done so far is to add this shell object to the argument list of a listener handler which I use. Should I be adding all the controls I need to this handler argument list??? It could get a little nasty for the application scallability...

I guess what I'm asking is how is the standard procedure for accessing anything related to the GUI, from outside the class that designs it. I want to separate the design and the program flow... You may answer me this is bad idea as well.

Thanks!
 
Mariano Lopez-Gappa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to hide the implementation details of the GUI as much as possible. So instead of passing around the Shell and the Label, you want to code to a more abstract interface.

The first thing to do would be to generate an Interface that defines methods used to set/get values you want. For example:


You want to code part of your application that sets the text of interest to this interface. In my example I am using an ActionListener because it does its work based on response to a button push.


The DataSetter here doesn't have to know anything about the GUI implementation - just the interface
.
Now you want to code your GUI so that it implements DataHolder interface. That way the GUI code can react to the DataSetter, without giving away any of the GUI details:


Now we can clean test it out with a simple window that causes the label values to change.


With the above code, the LabelSetter has a button that changes a value in a JLabel in the LabelHolder class. It uses an ActionListener - DataSetter - to do the work. The DataSetter doesn't know much about LabelHolder, it doesn't know that LabelHolder even has a GUI. It just knows it has the methods defined by DataHolder, effectively splitting the details from the interface (making it easier to change GUI implementations later on).
 
Mariano Lopez-Gappa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's incredibly useful thanks!!
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have another forum where this sort of question is usually discussed. Moving.

And welcome to JavaRanch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!