Frame closing in java

Java Frame Close WindowListener

The frame created by the programmer and displayed on the monitor comes with a title bar and three icons(buttons) on the title bar – Minimize, Maximize and Close. The first two works implicitly and the Close does not work and requires extra code to close the frame.

Java Frame Closing – 4 styles

Four styles exists to close the frame and are discussed one-by-one in this tutorial.

The last style is used very often as it requires less code to write. Now let us see how to close the window using the listener.

Java Frame Close WindowListener

In the following program, WindowListener interface is implemented to close the frame. The seven abstract methods of WindowListener interface are overridden.

import java.awt.Frame; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; public class FrameClosing1 extends Frame implements WindowListener < public FrameClosing1() < addWindowListener(this); setTitle("Frame closing Style 1"); setSize(300, 300); setVisible(true); >// override the 7 abstract methods of WindowListener public void windowClosing(WindowEvent e) < System.exit(0); >public void windowOpened(WindowEvent e) < >public void windowClosed(WindowEvent e) < >public void windowActivated(WindowEvent e) < >public void windowDeactivated(WindowEvent e) < >public void windowIconified(WindowEvent e) < >public void windowDeiconified(WindowEvent e) < >public static void main(String args[]) < new FrameClosing1(); >>

The FrameClosing1 class extends Frame and implements java.awt.event.WindowListener interface. WindowListener is capable to close the window when Close icon on the title bar is clicked.

addWindowListener() method of Frame (inherited from Window) registers or links the frame with the WindowListener. The parameter «this» represents an object of WindowListener (or to say, an object of FrameClosing1 that implements WindowListener).

Out of the 7 methods of WindowListener, only one method windowClosing() is overridden with code and the other 6 are given empty implementations (empty body). All the seven methods takes a parameter of WindowEvent object.

Источник

Setting the Behavior When Closing a JFrame

There are a number of techniques that one can use to close a frame in Java. Each has its advantages and disadvantages. Three techniques are presented below in general decreasing preference order:

JFrame.defaultCloseOperation(int option)

This is the simplest technique. All you need to do is to call the defaultClostOperation method of a JFrame , supplying the desired option value.

The possibilities for the option value are:

  • JFrame.EXIT_ON_CLOSE — A System.exit(0) call will be executed, exiting the entire application.
    • Do NOT use this option if the program is running as an applet as this will cause the browser to crash!

    The advantage of the technique is that it is very simple and easy, but it does not allow much flexibility to do any custom operations during the frame closing.

    public class MyFrame extends JFrame public MyFrame() < setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); >. >

    For dynamically configurable frame closing behavior, supply the option value to the constructor of the frame. This would be done by the controller, which would instantiate the frame with either an EXIT_ON_CLOSE option if it was an application or a DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE if it were an applet.

    java.awt.Window.addWindowListener(java.awt.event.WindowListener l)

    (Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

    In this technique a WindowListener interface implentation is added to the frame, where the listener has a method, windowClosing() , that is called when the frame is closed.

    In practice, on overrides the windowClosing() method of WindowAdapter , a no-op implementation of WindowListener . This way, one doesn’t have to worry about all the other methods of the WindowListener .

    public class MyFrame extends JFrame public MyFrame() < addWindowListener(new java.awt.event.WindowAdapter() < public void windowClosing(java.awt.event.WindowEvent e) < System.exit(0); >>); > . >

    This technique has the advantage that any sort of custom processing can be done when the frame is closed. The frame can be constructed with a command that the windowClosing method calls or with an complete WindowListener object, thus enabling dynamic configuration of the frame closing behavior. This is very useful when you want to run the same frame as either a regular application or as an applet — the controller will instantiate the frame with the proper closing behavior.

    The disadvantage of this technique is the larger code required than the first technique for standard window closing behaviors.

    java.awt.Window.processWindowEvent(java.awt.event.WindowEvent e)

    (Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

    In this technique, the processWindowEvent method, which is called when the frame is closed, is overriden to provide the desired behavior. The supplied WindowEvent parameter is tested to see if it is the event for the window is being closed and if so, the desired behavior is executed.

    public class MyFrame extends JFrame public MyFrame() < protected void processWindowEvent(WindowEvent e) < super.processWindowEvent(e); if(e.getID() == WindowEvent.WINDOW_CLOSING) < System.exit(0); >> > . >

    For dynamic behavior, a command supplied to the constructor of the frame can be run when the WINDOW_CLOSING event is detected.

    The advantage of this technique is that an entire object to handle the frame closing needs to be constructed but at the expense of a more complicated logical process.

    Источник

    How to Exit JFrame on Close in Java Swing

    In this tutorial, we will focus on learning how to exit JFrame on close in Java Swing. I will be discussing various methods in this tutorial in detail.

    Exit JFrame on Close in Java Swing

    We need to use the Java Swing library to create a JFrame, thus we must first import the library into our code. The Java Swing Library is used to create window-based applications with various powerful components such as JFrame which we will be using to create a frame.

    Do read this article, which clearly explains the Java Swing Library, its components such as JLabel, the creation of a JFrame, inbuilt- methods, its parameters, etc.

    Creating a Java JFrame

    JFrame is a class of the javax.swing package which is a container that provides a window on the screen. We create a JFrame with the title “Exit on Close Example” and use the setVisible(true) function to display the frame. We specify the location of the frame using the setBounds() function.

    Let’s see the code:

    JFrame frame = new JFrame("Exit on Close Example"); // Creation of a new JFrame with the title frame.setVisible(true); // To display the frame frame.setBounds(100, 200, 350, 200); // To set the bounds of the frame.

    How to Exit JFrame on Close in Java Swing

    We have created a plain empty JFrame with our title successfully. Now, we have to exit JFrame on close. One simple way of doing this is clicking the cross button on the top right corner of the frame which closes the frame but the application or code will still be running in the background.

    The efficient way of closing the JFrame is that we use the inbuilt method provided by the JFrame class i.e., using the JFrame.setDefaultCloseOperation(int) method. We use this method to change the behavior of the default JFrame which hides it instead of disposing of it when the user closes the window.

    Two Approaches on Closing JFrame

    Method 1: Using the WindowConstants

    The simplest and most used technique is to use WindowConstants.EXIT_ON_CLOSE as a parameter. We must extend the JFrame class to our main class to use this. By using this, it closes the JFrame window completely and also frees the memory.

    Method 2: Using the JFrame Constants

    We have various constants defined by javax.swing.JFrame to close the JFrame as per our requirements. They are :

    • JFrame.EXIT_ON_CLOSE — A System.exit(0) will be executed which will exit the entire code and the frame.
    • JFrame.DISPOSE_ON_CLOSE — It will dispose of the frame but keep the code running.
    • JFrame.DO_NOTHING_ON_CLOSE — The frame will not be closed. It basically does nothing.
    • JFrame.HIDE_ON_CLOSE — This is the default one. It will hide the frame but keep the code running.

    I have implemented both methods in the code for greater understanding.

    Let’s see the code:

    import javax.swing.*; public class ExitOnClose extends JFrame < public static void main(String[] args) < JFrame frame = new JFrame("Exit on Close Example"); // Creation of a new JFrame with the title frame.setLayout(null); // To position our components frame.setVisible(true); // To display the frame frame.setBounds(100, 200, 350, 200); // To set the locations of the frame. //------------------------------------------------ //To Terminate the JFrame and the code on close //------------------------------------------------ //METHOD-1 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //METHOD-2 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); >>

    Thanks for reading! I hope you found this post useful!

    Источник

    How to Close a Window in Java

    wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 10 people, some anonymous, worked to edit and improve it over time.

    This article has been viewed 143,260 times.

    This article will show you how to close a window in Java. Closing a window is much easier using Swing’s JFrame , but it’s also doable using AWT’s Frame .

    Using javax.swing.JFrame

    Image titled Close window java step1.png

    Image titled Close window java step2_with_import.png

Оцените статью