Article View: borland.public.jbuilder.java.swing
Article #9201Re: Closing a JFrame from another class
From: Paul Furbacher [TeamB]
Date: Thu, 11 May 2000 00:00
Date: Thu, 11 May 2000 00:00
57 lines
2945 bytes
2945 bytes
Song Huang wrote:
>
> "John P. McGrath" wrote:
>
> > [...]
> > Another would be to add a method that just closes the GUI:
> >
> > void closeGui() {
> > myGui.setVisible( false );
> > }
> >
> > [...]
> >
> > If you have any doubts, I would recommend starting with approach #2.
>
> The problem is: if i use approach #1, it will require mygui to be the class
> variable in the UsingMyGui class(which i have mislead you in the example).
>
> approach # 2, i don't think it is doable. because i don't have access to the
> mygui variable.
Sure you do if you have designed your application correctly. You are
in charge of what you have access to and what you don't.
> what i am thinking about is having the operating system(ms windows) to
> close/dispose it. ...
That's not worth investigating -- period.
As John pointed out, you can approach the problem from several angles.
It's up to you to include references in your classes so that you can
do what you want.
An important aspect of writing applications is to conceive of them
in the same Model-View-Controller (MVC) paradigm that underlies the design
of the UI components you are using. At some point, you should design
a "controller" class which is responsible for instantiating the views
(frames and dialogs of your application) and controlling their visibility.
The control acts as a "listener" to requests from those views for data
which fills the sub-views (i.e., controls such as listboxes, tables,
etc.)
of those views.
Hence, as a view controller, this class would maintain references to all
views (frames and dialogs) in your application. If the controller class
receives a message to close one of those windows, the controller checks
to see if the reference to that view is not null, and closes it. In this
particular case, your controller class definitely "knows" about the
instance of
"MyGUI" (you could think about giving this a better class name, even
"MyFrame" is better than "MyGUI"!) because you, the programmer, are in
control of writing the controller class.
This MVC approach to application design is an important concept to
incorporate into your way of thinking. It takes work and practice.
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<391B232A.8718B6BD@home.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3919CF22.3BAB6561@mindspring.com> <MPG.1383f716e6b0db2998990d@forums.borland.com> <391AC9FC.C79095C6@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: "Paul Furbacher [TeamB]" <pnf@home.com> Subject: Re: Closing a JFrame from another class Date: 2000/05/11 Message-ID: <391B232A.8718B6BD@home.com> X-Deja-AN: 622283426 Content-Transfer-Encoding: 7bit References: <3919CF22.3BAB6561@mindspring.com> <MPG.1383f716e6b0db2998990d@forums.borland.com> <391AC9FC.C79095C6@mindspring.com> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pnf@home.com Newsgroups: borland.public.jbuilder.java.swing