🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #227

Re: Accessing Components of top-level Frame?

#227
From: Paul Furbacher [TeamB]
Date: Tue, 02 Jan 2007 04:09
48 lines
2489 bytes
Tom Giammo wrote:

> [...]
> 
> Once I realized this, I wrote the following general method to fetch the 
> top-level frame of any component:
> 
>     public Frame getEnclosingFrame(Component c) throws NoSuchMethodException 
> {
>         Component p = c;
>         Class fClass = java.awt.Frame.class;
>         while (!fClass.isInstance(p)) {
>             String pName = p.getClass().getName();
>             p.getClass().getMethod("getParent", null);  // throws exception 
> if appropriate
>             p = ((Component) p).getParent();
>         }
>         return (Frame) p;
>     }
> 
> This method worked without any problem to get me to my statusBar component, 
> i.e. :
> 
>             frameMain myFrame = (frameMain)getEnclosingFrame(this);
>             myFrame.statusBar.setText(statusMessage);
 > [...]

It may work, but it's far too clever.

The observer pattern suggested in a sibling post is far
more common an approach to this very common problem.
Reflection is the wrong tool here.


-- 


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: <4599dac7$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <459802c2$1@newsgroups.borland.com> <45984988$1@newsgroups.borland.com> <4599493d@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:09:45 -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> <45984988$1@newsgroups.borland.com> <4599493d@newsgroups.borland.com>
In-Reply-To: <4599493d@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: <4599dac7$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1167710919 69.142.194.228 (1 Jan 2007 20:08:39 -0800)
Lines: 48
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail