How to Open a new JFrame on Button Click in Java

Hello Friends, welcome to another tutorial and in this tutorial, we will learn How to Open a new JFrame on Button Click in Java Swing. In the previous tutorial, we have already learned how to do a Button click event in Java Swing and using the concept, we will learn how to open a new JFrame when we click on the button in Java.

So without further ado, Let’s start our tutorial, How to Open a new JFrame on Button Click in Java Swing, step by step.

Preview

How to Open New JFrame on Button Click in Java
Preview

How to Open a new JFrame on Button Click in Java?

First of all, we will create our First JFrame and in which we will add a Label and Button into it. So Let’s start to design our First JFrame step by step.

Importing Packages

So the first step is to import all the necessary packages in our program so that we will be able to use the classes and interfaces inside that package into our program.

Creating Class FirstFrame.java

  • Now will create a class named FirstFrame, and inside that class, we will create the main method of the class.
  • Next, we will create the constructor of our class.

Creating First Window and Adding Components

  • In this step, we will create the First Window using JFrame and set its properties, and then we will add two components to it, one JLabel and one JButton.
  • Next, we will set the properties of JLabel and JButton, and then lastly, we will add them to the JFrame.
  • For this,
    • Create the object of JFrame and the components inside the class.
    • Set the properties of JFrame and the components inside the constructor of the class.
    • Next, Create the object of the class inside the main method.
  •  Now Run your program.
How to Open New JFrame on Button Click in Java
How to Open a New JFrame on Button Click in Java – fig – 1
  • As you can see that we have successfully designed our First JFrame.
  • Now we will add some button click event to our JButton so that whenever we click on the JButton, some activity should be performed.

Implementing ActionListner Interface in FirstJFrame

  • For this, we need to implement the ActionListener interface in our class and override its abstract method into that class which is actionPerformed() method.
  • Then next, we will register the component using the addActionListener() method. The Parmater of the addAcctionListener() method is the object of the class in which the ActionListener interface is implemented, and since we are in the same class so we will pass this as its parameter.
  • Next, we will code the desired code inside the actionPerformed() method, which means the activities we want to happen when the button clicks. In this case, we want a new JFrame to be opened when someone clicks on the button, But we have not created our Second JFrame, which we want to display when someone clicks on the button. So we will create the Second JFrame in a separate class and design it just like we have designed the First JFrame.

Creating Class SecondJFrame

  • In this step, we will create a separate class named SecondJFrame within the same package, and inside that class, we will create the constructor of this class.

Creating Second Window and Adding Components

  • In this step, we will create the Second Window using JFrame and set its properties, and then we will add two components to it, one JLabel and one JButton.
  • Next, we will set the properties of JLabel and JButton, and then lastly, we will add them to the JFrame.
  • For this,
    • Create the object of JFrame and the components inside the class.
    • Set the properties of JFrame and the components inside the constructor of the class.

Implementing ActionListner Interface in SecondJFrame

  • Now, we want when someone clicks on the button inside the Second JFrame, then the First JFrame should be opened.
  • For this, we will implement the ActionListener interface in our class and then override its abstract method actionPerformed() into the class in which the ActionListener interface is implemented.
  • Then next, we will register the component using the addActionListener() method. The Parmater of the addAcctionListener() method is the object of the class in which the ActionListener interface is implemented, and since we are in the same class so we will pass this as its parameter.

Final Step

  • Now in the final step, what we want to do is when the button inside the First JFrame is Clicked, then the Second JFrame should be opened, and the First JFrame should be disposed, and when the button inside the Second JFrame is clicked, then the First JFrame should be opened and the second JFrame should be disposed.
  • For this, we will code the desired code inside the actionPerformed() method.
  • To open the particular JFrame, we will create the object of that particular class, and to dispose the JFrame, we will call the dispose() method using the object of that JFrame.

Final Code

After putting all together, the final code of How to Open a New JFrame on Button Click in Java is given below.

  • Now run your program.
How to Open a New JFrame on Button Click in Java – fig – 2
  • Now click on the Button to Open the new JFrame.
How to Open a New JFrame on Button Click in Java – fig – 3
  • Now you can again click on the button to go back to the First JFrame.

That’s it, everyone. If you have any questions or doubts about this post, please leave a comment below.


Related Articles…

Leave a Comment