How to close the JFrame window programmatically in Java

In this tutorial, we will learn the various methods and techniques by which a JFrame window can be closed programmatically in Java.

There are many ways by which we can close the JFrame window programmatically in Java. But yes closing a JFrame is difficult to get into the head because there are a lot of variants on how we can do it.

Some of the primary ways can be :

  • Make the JFrame window invisible and keep it ready to be made visible again
  • Make the JFrame Window invisible and store it in a cold storage
  • We can also close the window by clicking on the cross button X. While clicking the X button at the top right corner, the JFrame window programmatically is sent to the window closing event.

So we discussed some basic ideas about how to close the JFrame window in Java. Let’s categorize them in a proper fashion:

  • Final Close: When you want to get rid of the JFrame window and never want to use it again then call the dispose() function immediately after the setVisible ( false ) is called. This is done to avoid memory leaks since Java GUI is not so smart enough to do its own garbage collection

Code:

DemoJframe(){

        setTitle("How to Close The JFrame Window Programmatically in java");
        Font myFont1 = new Font("Serif", Font.BOLD, 25);

        setBounds(400,200,700,500);
        setLayout(null);

        JButton b1=new JButton("Click to close");
        b1.setBounds(140,200,100,30);
        add(b1);
        setVisible(true);
        b1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                
                dispose();
                setVisible(false);
                
            }
            
        });
    }
  • Temporary Close: When you want to close the window for a short period of time then you can call the setVisible (false) function to make the window invisible and to bring it back call setVisible to true.
  • ShortHand WindowClosingEvent handler: There are in total 3 shortcuts to execute the closing operation of a JFrame Window.

(1)  If the programmer wants to dispose of the window then he/she can write this code instead to invoke the window closing event handler.

    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

(2) If the programmer wants to hide the window temporarily, then he/she can write this code instead to invoke the window closing event handler

    this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)

(3) If the programmer wants to permanently close the window call this code.

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Hope this information will help you all a lot in learning JFrame and Java swing.

Thank you,

Happy Coding !!

One response to “How to close the JFrame window programmatically in Java”

  1. Senjyoti Ghosh says:

    Thank you for thissss…. it helped me a lot to understand javaaa… thanku☺

Leave a Reply

Your email address will not be published. Required fields are marked *