Article View: borland.public.jbuilder.java.swing
Article #9191Re: Closing a JFrame from another class
From: John P. McGrath
Date: Wed, 10 May 2000 00:00
Date: Wed, 10 May 2000 00:00
46 lines
1711 bytes
1711 bytes
On Wed, 10 May 2000 17:05:38 -0400, Song Huang wrote:
> Hello
>
> i have a question about closing a window(JFrame). Let's say:
>
> public class MyGui extends JFrame{//this is a GUI
> ..............
> }
>
> public class UsingMyGui{
> ...
> MyGui mygui = new MyGui();
> mygui.setVisible(true);
> ...
> }
>
> Now i want to close this GUI from another class(which doesn't has access
> to the mygui variable). How can i do that?
You need to add a method to UsingMyGui that allows the other class to do
this. One way would be to add a "getter" to UsingMyGui to get the MyGui
object:
MyGui getGui() {
return myGui;
}
Another would be to add a method that just closes the GUI:
void closeGui() {
myGui.setVisible( false );
}
Which should you use? That depends a lot on the view that UsingMyGui
wants to present to the outside world. Is the MyGui object simply part
of the implementation of UsingMyGui? Should others know that it has a
MyGui object, or should they just know that there is some sort of GUI
that can be closed?
If you have any doubts, I would recommend starting with approach #2.
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.1383f716e6b0db2998990d@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3919CF22.3BAB6561@mindspring.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,46f628b1b7c9f8ec X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Closing a JFrame from another class Date: 2000/05/10 Message-ID: <MPG.1383f716e6b0db2998990d@forums.borland.com> X-Deja-AN: 621868146 References: <3919CF22.3BAB6561@mindspring.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing