How to Close the Current Form in JAVA Swing

You see many people asking how to close only the current form in Java Swing, so write this.
The main interface consists of two JButtons, one to invoke another JFame with a button event, and the other to close the current form.
1. The method setDefaultCloseOperation(jframe. EXIT_ON_CLOSE) cannot be used to close the current form, but setDefaultCloseOperation(jframe. DISPOSE_ON_CLOSE) can be used;
2. Exit() cannot be used through the JButton event, which will cause the entire form of the program to close, dispose () can be used; This closes only the current formSpecific implementation is as follows:
NewFrame.java

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewFrame extends JFrame {

	/**
	 * called another JFrame
	 * close this JFrame
	 * write by Jimmy.li
	 * time:2016/4/6 22:55
	 */
	private static final long serialVersionUID = 1L;

	public NewFrame() {
		// Normal button control
		JFrame jf = new JFrame("main");
		Toolkit tk = this.getToolkit();// Getting the Window Toolbar
		int width = 650;
		int height = 500;
		Dimension dm = tk.getScreenSize();
		jf.setSize(300, 200);// Set the size of the program
		jf.setLocation((int) (dm.getWidth() - width)/2,
				(int) (dm.getHeight() - height)/2);// Appears in the center of the screen
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jf.setVisible(true);
		JPanel contentPane = new JPanel();
		jf.setContentPane(contentPane);

		// Create two buttons and add them to the content panel.

		JButton another = new JButton("Start Another Page");

		JButton close = new JButton("close");

		contentPane.add(another);

		contentPane.add(close);

		another.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new exit();
			}
		});

		close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}

	public static void main(String[] args)

	{
		new NewFrame();

	}
}

The renderings are as follows:

Close only exit’s form, not the parent form.
The exit.java code is as follows

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * called another JFrame close this JFrame write by Jimmy.li time:2016/4/6 22:55
 */

public class exit {

	private static final int WIDTH = 300;

	private static final int HEIGHT = 200;

	public exit() {
		// Normal button control
		final JFrame jf = new JFrame("exit");
		jf.setSize(WIDTH, HEIGHT);
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jf.setVisible(true);
		JPanel contentPane = new JPanel();
		jf.setContentPane(contentPane);

		// Create two buttons and add them to the content panel.

		JButton close1 = new JButton("Closed");
		contentPane.add(close1);

		close1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// System.exit(0);
				jf.dispose();
			}
		});
	}

	public static void main(String[] args)

	{
		new exit();
	}

}

 

By clicking the close button, only the current exit form is closed and the parent form still exists.

Good luck!

Read More: