Reputation: 891
I have a java application, want to close the GUI with confirmation window to close the application
for example:-
frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frmViperManufacturingRecord.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
JFrame frame = (JFrame)e.getSource();
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
This is working fine, when I press the window close (x) button, but I want to bring this event to a button to perform the action 'On Click', since I am newbie finding difficulties to bring it inside the 'actionPerformed'
So far I have tried the code below and it didn't work...
//close window button
JButton btnCloseWindow = new JButton("Close Window");
btnCloseWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frmViperManufacturingRecord.dispose();
//System.exit(0);
JFrame frame = (JFrame)e.getSource();
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
Please give me some directions on this, thanks
Upvotes: 0
Views: 3355
Reputation: 1420
Change this:
if (result == JOptionPane.YES_OPTION)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
to
if (result == JOptionPane.YES_OPTION)
System.exit(0);
}
frame.setDefaultCloseOperation
is what happens when someone clicks the 'x' to close the window. Every other way to exit is controlled by you. The best way is to have your window closing listener and action listener call the same method that will pop up the dialog and call System.exit(0)
if the user wants to exit. This will also help you with cleanup operations.
Sample code:
public class Test extends JPanel implements WindowListener {
public Test() {
setLayout(new BorderLayout());
add(new JLabel("This is a test."), BorderLayout.CENTER);
JButton b = new JButton("Exit");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit();
}
});
add(b, BorderLayout.SOUTH);
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
Test t = new Test();
f.add(t, BorderLayout.CENTER);
f.addWindowListener(t);
f.pack();
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
exit();
}
private void exit() {
System.exit(0);
}
}
Upvotes: 3
Reputation: 936
You can try:
if (result == JOptionPane.YES_OPTION){
frame.dispose();
}
Also note CastException on the line 122.
Instead of
JFrame frame = (JFrame)e.getSource();
change to:
JFrame frame = new JFrame();
Upvotes: 1
Reputation: 464
if still not working then try
frame.setVisible(false);
frame.dispose();
in if(result == JOptionPane.YES_OPTION){} block
Upvotes: 1