• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SWT: access to controls from outside main

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's incredibly useful thanks!!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have another forum where this sort of question is usually discussed. Moving.

And welcome to JavaRanch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic