How do I handle a window closing event?
Category: javax.swing, viewed: 740 time(s).
Here you'll see how you can handle the window closing event of a JFrame. What you need to do is add a WindowListener to the frame instance and implement the windowClosing() method.
package org.kodejava.example.swing; import java.awt.Button; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class WindowClosingExample extends JFrame { public static void main(String[] args) { WindowClosingExample frame = new WindowClosingExample(); frame.setSize(new Dimension(200, 200)); frame.add(new Button("Hello World")); // // Add window listener by implementing WindowAdapter class to the // frame instance. To handle the close event we just need to implement // the windowClosing() method. // frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Show the frame // frame.setVisible(true); } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I right justified JTextField contents?
- How do I format JLabel using HTML?
- How do I display an image in JButton?
- How do I use JFormattedTextField to format user input?
- How do I handle mouse button click event?
- How do I handle mouse wheel event?
- How do I handle mouse motion event?
- How do I handle mouse event in swing?
- How do I get the system look and feel?
- How do I set the look and feel for swing application?