Article View: borland.public.jbuilder.java.swing
Article #226Re: Accessing Components of top-level Frame?
From: Paul Furbacher [TeamB]
Date: Tue, 02 Jan 2007 04:00
Date: Tue, 02 Jan 2007 04:00
75 lines
4161 bytes
4161 bytes
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
http://java.sun.com/docs/books/tutorial/uiswing/events/index.html
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:
http://info.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<4599d8ae$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!amsnews11.chello.com!feeder1.cambrium.nl!hwmnpeer01.ams!news.highwinds-media.com!news.astraweb.com!newsrouter-eu.astraweb.com!193.201.147.67.MISMATCH!feeder2.cambrium.nl!feed.tweaknews.nl!hitnews.eu!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mailReferences:
<459802c2$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,d29c312c94108c85 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 01 Jan 2007 23:00:47 -0500 From: "Paul Furbacher [TeamB]" <pfurbacher@mac.com> Organization: Reverse Alchemy, Inc. User-Agent: Thunderbird 1.5.0.9 (Macintosh/20061207) MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Accessing Components of top-level Frame? References: <459802c2$1@newsgroups.borland.com> In-Reply-To: <459802c2$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.142.194.228 Message-ID: <4599d8ae$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1167710382 69.142.194.228 (1 Jan 2007 19:59:42 -0800) Lines: 75 Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!amsnews11.chello.com!feeder1.cambrium.nl!hwmnpeer01.ams!news.highwinds-media.com!news.astraweb.com!newsrouter-eu.astraweb.com!193.201.147.67.MISMATCH!feeder2.cambrium.nl!feed.tweaknews.nl!hitnews.eu!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail