0

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...

6
Contributors
13
Replies
16
Views
5 Years
Discussion Span
Last Post by hoon85
Featured Replies
  • If you dont want the window not to close when user selects NO or CANCEL. you should use [CODE]setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);[/CODE] Read More

0

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

Have you looked at the setDefaultCloseOperation method?

Edited by NormR1: n/a

0

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...

Attachments screen.jpg 23 KB
0

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

0

@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.

0

parameter: EXIT_ON_CLOSE

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0

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

1

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

you should use

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
0

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

0

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;
}

}
0

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);

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??

Edited by hoon85

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.