1.in my project...if user want to exit from the application... it first check yes no cancel option,if the user want to choose ,no or cancel button .. the application is not closed..but my problem is what code placed for no or cancel button...to prevent closing window...


code is...

class wl extends WindowAdapter{

public void windowClosing(WindowEvent e){
System.out.println("closing window"+e);
int result=JOptionPane.showConfirmDialog(null, "Are you sure to exit?","Confirm Exit", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE );


if (result==JOptionPane.YES_OPTION){
System.exit(0);
}

if (result==JOptionPane.NO_OPTION){
//??????
}

if (result==JOptionPane.CANCEL_OPTION){
//??????
}

}

2.what is the use of dispose method in JFrame...

Recommended Answers

All 13 Replies

what code placed for no or cancel button...to prevent closing window..

Have you looked at the setDefaultCloseOperation method?

no...if select a close option in a frame(minimize,resizable,close), it will call the ask confirm exit, if yes means , use the system.exit() and then close the frame, if am not want to close(choose no or cancel option) then what should i do for not closing the frame???

screen shot attached...

Which parameter are you using with the setDefaultCloseOperation method?
If you return the window remains open.

@sathya88

I think this is what you are looking for

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

place the above code in line 13 and 17.

parameter: EXIT_ON_CLOSE

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

If you use that one the window will close. Look at the JFrame API doc and find one that will NOT close the window.

If you dont want the window not to close when user selects NO or CANCEL.

you should use

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

ya..DO_NOTHING_ON_CLOSE Parameter... program working fine...

For no option you can go to another o frame and for cancel option you can do this....

class wl extends WindowAdapter{

public void windowClosing(WindowEvent e){
System.out.println("closing window"+e);
loop:
int result=JOptionPane.showConfirmDialog(null, "Are you sure to exit?","Confirm Exit", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE );


if (result==JOptionPane.YES_OPTION){
System.exit(0);
}

if (result==JOptionPane.NO_OPTION){
this.setvisible(false);
new frame_name().setvisible(true);
}

if (result==JOptionPane.CANCEL_OPTION){
go to loop;
}

}

what is the use of dispose method in frame????

What does the API doc say it does?

dispose(), really nothing for JFrame (JDialog, JOptionPane or JWindow)'s lifecycle, that's same as setVisible(false)

for JFrame is there System.exit(0); or JFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); these two methods always close all Object, kill the current JVM Instance,

that valid only for JFrame, because has implemented method finalize(), another Top-Level Containers (JDialog, JOptionPane, JWindow) miss this method and current instance must be closed by using System.exit(0);

Do you find out answer??
I have a some problem same with you
Please give me a answer~~!!
I couldn't find

And

1. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Does it work??

how it works??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.