• 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

swing: listener/oberver

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a standalone runnable class ("extends runnable") that does stuff. for this point on I'll refer to it as AClass

I want to extend AClass so that users, using a SWING GUI, can interact with AClass during its execution. The major obstacle I am having is that AClass needs to send messages back to the UI so the user knows what is going on.

Near as I can figure my choices are:
1) to make AClass Observable and then have the GUI observe AClass.
2) to pass a reference for the GUI to AClass and then then AClass
directly updates the ui.( Not particually happy about adding UI
code to my class)

Am I missing other options?

I tried to find the pro/cons of both approaches on google and ask.com.(failed) which is better and why?

thanks for any help
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion the Observable solution is the better one. You do not clutter you AClass's code with gui stuff and that seems to be best.

You can use any Event/Listener principle you'd like. You might want to take a look at PropertyChangeListener/EventListener. You can even create your own eventlistener interface that your GUI must implement. Your AClass can have add and remove methods for those listeners.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I found the Observer/Observable stuff in the library I wrote my own little Pub-Sub manager. I have a couple programs that work like this:

I'm sure you could adapt the ideas to Observer or manage your own events. Are you familiar with SwingUtilities and getting your UI updates back onto the AWT thread?
 
peter cooke
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am primarily a J2EE jsp/servlet programmer with a smattering of standalone server applications. This is my first non J2EE user interface. One of the java programmers at work with more awt/swing experience say that I should
have my standalone class run in the context of an AWT/SWING thread but
that did not make any sense to me. I thought that what is reffered to as an "awt thread" is used for painting/drawing stuff in various components/panels.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All your Swing listeners and event handlers and such run on the AWT or Swing thread. If you do something very fast, like clicking a radio button makes a field invisible, you can do the work on that thread.

If you do something that runs more than a fraction of a second the user interface can become noticeably slower or unresponsive for a period of time. Then it's good to shove the work off on another thread. And then you have the issue of getting UI updates back on the AWT thread, which is the whole purpose of the SwingUtilities invokeAndWait() or invokeLater() methods.

I have a program that runs any number of file downloads in separate threads. Each downloader thread publishes status events (every 1k bytes or on error) to an updater, which then uses invokeLater() to put an update command on the AWT thread. Just like the picture above.

Does that help?
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic