Article View: borland.public.jbuilder.java.swing
Article #223Re: Accessing Components of top-level Frame?
From: Paul Nichols TeamB
Date: Sun, 31 Dec 2006 23:37
Date: Sun, 31 Dec 2006 23:37
100 lines
4382 bytes
4382 bytes
>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:
<45984988$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!news.glorb.com!feed.xsnews.nl!feeder.xsnews.nl!192.87.166.28.MISMATCH!tudelft.nl!txtfeed1.tudelft.nl!feeder.news-service.com!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!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: Sun, 31 Dec 2006 18:37:21 -0500 From: "Paul Nichols [TeamB]" <"Paul Nichols [TeamB]"> Reply-To: "Paul Nichols [TeamB]" User-Agent: Thunderbird 1.5.0.8 (X11/20061117) 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: 70.34.189.24 Message-ID: <45984988$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1167608200 70.34.189.24 (31 Dec 2006 15:36:40 -0800) Lines: 100 Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!news.glorb.com!feed.xsnews.nl!feeder.xsnews.nl!192.87.166.28.MISMATCH!tudelft.nl!txtfeed1.tudelft.nl!feeder.news-service.com!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail