Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Re: Accessing Components of top-level Frame?

From:"Paul Furbacher [TeamB]" [pfurbacher@masked-domain]
Sent on:Mon, 01 Jan 2007 23:00:47 -0500
In reply to:Accessing Components of top-level Frame? sent on Sun, 31 Dec 2006 13:35:26 -0500 by "Tom Giammo" [crestline@masked-domain]
Tom Giammo wrote:
> [...]
> I'm stuck on trying to alter the text of a "statusBar" JLabel component that
> was set up for me on the top-level frame when I started a new application.
> [...]
> I would like to alter the text of "stausBar" from within another component
> that I've added to "frame", the instance of the top-level JFrame extension.
> I've tried several approaches to getting at "statusBar". Simply using its
> name (i.e. "frame.statusBar") doesn't work - presumably because "frame" is
> only a property of the generated application class (named, in this case,
> "hexGame").

This approach, even if you give the "statusBar" member variable public
access, or create a getter method for it in the frame, is too tightly
coupled. This "other component" has to know too much about its
context -- the frame and its status bar -- such that if you wanted
to use it in another context, one without a status bar but may be
some kind of text display object in a panel in a panel in a frame,
you'd have to do some rewriting. [See below for a general approach
to solving this very common problem.]

> The instance of hexGame, however, has not been given a name by
> JBuilder - it is simply generated in the main() method by "new hexGame()"
> without any assignment - and so I can't prefix its name to
> "frame.statusBar".

What JBuilder generates for you is just a starting point.
You modify that to suit your purposes. In this case, there's
absolutely nothing preventing you from naming that variable,
hoisting its declaration, changing it to a member variable, and
even changing its scope. It's up to you, not the IDE.

> I've tried [...]

I think you'd do better to look into "listeners", an
implementation of the "observer" pattern, which Sun
uses extensively in the Swing framework. Based on
the concept, you'd develop a event-listener model for
updating your status bar.

Beside Googling

+listener +tutorial +sun +java

you could start here

You would add to your component the ability to register (add)
listeners for update events, and when the status of your component
changed, you'd broadcast to those listeners the display text.

Your frame would implement your custom listener interface, and the
method which is called would, in this particular case, update the
status bar. Your application object, when it creates both the
frame and the "other component", would add the frame as a listener
to that component.


Paul Furbacher (TeamB)

Save time, search the archives:

Is it in Joi Ellis's Faq-O-Matic?

Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Found what you were looking for? If not - continue at Wiki Index

Other messages in this topic

SenderDate sentSubject
"Paul Nichols [TeamB]" <"Paul Nichols [TeamB]">Mon, 01 Jan 2007 18:21:39 -0500Re: Accessing Components of top-level Frame?
"Tom Giammo" [crestline@masked-domain]Mon, 1 Jan 2007 12:48:04 -0500Re: Accessing Components of top-level Frame?
"Paul Nichols [TeamB]" <"Paul Nichols [TeamB]">Sun, 31 Dec 2006 18:36:40 -0500Re: Accessing Components of top-level Frame?
"Tom Giammo" [crestline@masked-domain]Sun, 31 Dec 2006 13:35:26 -0500Accessing Components of top-level Frame?

Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: Statistics for project etracker� web controlling instead of log file analysis