🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Accessing Components of top-level Frame?

#224
From: Tom Giammo
Date: Mon, 01 Jan 2007 17:48
153 lines
6586 bytes
Paul:

Thanks for responding.

Your approach is ingenious and no doubt would work.  I had begun to explore 
along those lines myself, but couldn't keep straight in my mind which 
variables would be visible to the components within the top-level frame.  I 
ultimately gave up on the basis that something so simple as accessing a 
sibling component of the same top-level couldn't possibly be so complex.  I 
also found the necessity of modifying the JBuilder generated code so 
significantly a bit distasteful.

Upon much reflection <pun intended>, I think that I've come up with a 
simpler way that is less disruptive of the JBuilder code.  I was on the 
right track when I tried to find the top-level frame containing my component 
using the component's "getParent()" method.  In this regard, I think that 
your comment on this approach ("Will not work, you are attempting to bypass 
encapsulation rules") is not correct.  The real problem lies in the fact 
that the immediate parent of my component was not the top-level frame 
itself, but rather was that frame's contentPane.  The frame object itself 
could only be reached by chaining up through the parents of its contentPane, 
JLayeredPane, and JRootPane containers.

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);

Tom Giammo



"Paul Nichols [TeamB]" <"Paul Nichols [TeamB]"> wrote in message 
news:45984988$1@newsgroups.borland.com...
> >Tom Giammo wrote:
>
>
>> 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.
>>
>
> Easy enough if statusBar is the name of your JLabel:
>
>  statusBar.setText("Some new text.");
>
>> 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").
>
> Unless the implementation of frame is static, that would be correct. 
> Children or child objects of the parent class are only accessible within 
> that class if they are declared private, only accessible by derived 
> classes within the same package if declared protected, and accessible by 
> all if declared "public".
>
>
> 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".
>
> And that is totally within the context of the rules concerning 
> encapsulation. Since the public static void main(String args[]) is 
> declaring this as a new variable for HexGame (presumably), then the 
> hexGame instance is only accessible within this method, static void 
> main().
>
>
> I've tried using the "parentFrame" property of the
>> component on the same "frame" from which I am trying to alter 
>> "statusBar". With the right castings, this compiles without a problem but 
>> causes an uncaught exception in the system's event queue.
>>
> Will not work, you are attempting to bypass encapsulation rules.
>> Everything else in my application works like a charm  - as long as I 
>> don't try to access "statusBar".
>>
>> I'm obviously missing something.  I'd appreciate what ever help anyone 
>> gan give.
>>
> You are. Here are several suggestions:
>
> /* (1) Make main a static member of the Main class and hexGame a member of 
> the Main Class */
>
>   public class Main{
>    private HexGame hexGame;
>    private static Main main;
>
>    //in the Main Class' public static void method
>    //add the main=New Main();
>
>       public static void main(String args[]) {
>         main= new Main();
>           main.hexGame = new HexGame();
>             //set any other attributes or run time methods
>
>       }
>
>     //add a method to get main
>         public static Main getMainClass(){
>          if(main==null) {
>             main=new Main();
>          }
>            return main;
>        }
>    //Add a method to get HexGame
>         public HexGame getHexGame(){
>           return hexGame();
>         }
>
>   //Now in your HexGame class, add a public method for settingText for 
> StatusBar
>
>       public class HexGame implements JFrame{
>
>        .....
>
>
>        public void setStatusBarText(String newText){
>            statusBar.setText(""); //make sure to clear text up
>   //to this point
>    statusBar.setText(newText);
>
>       }
>  .........
>   }
>
>    //Now in your other classes
>      private void someClassMethod(){
>        Main.getMainClass().getHexGame().setStatusBarText("Some new Text");
>      }
>
> That should do it for you! 
Technical Details
Message-ID: <4599493d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <459802c2$1@newsgroups.borland.com> <45984988$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
Reply-To: "Tom Giammo" <crestline@giammo.com>
From: "Tom Giammo" <crestline@giammo.com>
Newsgroups: borland.public.jbuilder.java.swing
References: <459802c2$1@newsgroups.borland.com> <45984988$1@newsgroups.borland.com>
Subject: Re: Accessing Components of top-level Frame?
Date: Mon, 1 Jan 2007 12:48:04 -0500
Lines: 155
Organization: Crestline Enterprises
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 69.138.228.103
Message-ID: <4599493d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1167673661 69.138.228.103 (1 Jan 2007 09:47:41 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail