I have an application. I need to make this, when user click the close button, a confirmation will come ( just like javascript confirm dialog ) , 'Are you sure'? I'm using Swing visual editor in Netbeans and I'm using a listener on window closing. But I can't make it. Any help?
Asked
Active
Viewed 3,113 times
2
-
2Please first search the forum as this has been asked and answered a gazillion times. For e.g., [how-can-a-swing-windowlistener-veto-jframe-closing](http://stackoverflow.com/questions/3777146/how-can-a-swing-windowlistener-veto-jframe-closing). Also you say that you're using a WindowListner (the correct approach), "but I can't make it" -- but then don't show us what you've tried. Please do so, else we can't tell you what you're doing wrong. – Hovercraft Full Of Eels Oct 08 '11 at 05:56
-
@HovercraftFullOfEels I couldn't find the correct keywords for my query at that time. Thanks for the suggestion ... – Dewsworld Oct 08 '11 at 06:13
-
You're welcome. Again, if after reviewing the link or similar ones and you're still stymied, then please show us your code and tell us any specifics on how it's not working such as error messages or wrong behavior. – Hovercraft Full Of Eels Oct 08 '11 at 06:14
-
@HovercraftFullOfEels Actually I was trying to implement the confirmation with the exit icon at top-right. If it was a button, there would be no problem. But how can I get a reference of that exit icon. That's why I didn't write any code... – Dewsworld Oct 08 '11 at 09:17
1 Answers
6
Try this code:
jj4.addActionListener(new ActionListener() // jj4-button's reference to implement exit
{
public void actionPerformed(ActionEvent ae)
{
if(!jtextarea.getText().equals("") && !jtextarea.getText().equals(fileContent))
{
if(fileName == null)
{
//this method have 3 options (1 = YES, 2 = NO, 3 = Cancel)
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
if(option == 0)
{
//to save the text into file
saveAs();
System.exit(0);
}
if(option == 1)
{
//to exit the program without saving
System.exit(0);
}
}
else
{
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
if(option == 0)
{
save();
System.exit(0);
}
if(option == 1)
{
System.exit(0);
}
}
}
else
{
System.exit(0);
}
}
});

shubhendu mahajan
- 816
- 1
- 7
- 16
-
4Don't use magic numbers! JOptionPane has defined varaiables to check for Yes, No and Cancel. – camickr Oct 09 '11 at 04:24