Link to home
Start Free TrialLog in
Avatar of jespersahner
jespersahner

asked on

Using setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) with multiple JFrame's

Hi!

I am running an application which uses multiple JFrame's running in separate threads. To close a JFrame I have set:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The problem now is, that closing one JFrame closes all JFrame's, and I only want to close the selected JFrame.

How do I solve that?

Regards,
Jesper
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

That causes the JVM instance to end when the frame is closed.

Generally, you'd just add a WindowListener to your JFrame, and then either destroy() it, or hide it using setVisible(false).
An example:

JFrame fr ;  // global
.
.
WindowListener l = new WindowAdapter()
{
    public void windowClosing( WindowEvent evt )
    {
        // close here
        fr.setVisible( false ) ;
        // or fr.destroy() ;    <-- probably the best one to use
    }
} ;
fr.addWindowListener( l ) ;
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i.e.

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Oh. That's easier :)
Avatar of jespersahner
jespersahner

ASKER

->CEHJ: Thanks! Reading the manual often solves the problem, which I didn't in this case :-))
-> InteractiveMind: Thanks for your input anyway :-)
:-)