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 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.
1 2 3 4 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class FirstJFrame { FirstJFrame(){ } public static void main(String[] args) { } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class FirstJFrame { JFrame frame; JButton openSecondFrame; JLabel label; FirstJFrame(){ frame=new JFrame("First JFrame"); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.PINK); frame.setResizable(false); label=new JLabel("This is First JFrame"); label.setBounds(140,100,200,30); label.setFont(new Font("Arial",Font.BOLD,20)); openSecondFrame=new JButton("Open Second JFrame"); openSecondFrame.setBounds(100,200,300,30); openSecondFrame.setFocusable(false); frame.add(label); frame.add(openSecondFrame); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { FirstJFrame firstJFrame=new FirstJFrame(); } } |
- Now Run your program.
- 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.
1 2 3 4 5 6 7 |
public class FirstJFrame implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { } } |
- 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.
1 |
openSecondFrame.addActionListener(this); |
- 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.
1 2 3 4 5 |
class SecondJFrame{ SecondJFrame(){ } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class SecondJFrame{ JFrame frame; JLabel label; JButton openFirstFrame; SecondJFrame(){ frame=new JFrame("Second JFrame"); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.CYAN); frame.setResizable(false); label=new JLabel("This is Second JFrame"); label.setBounds(140,100,230,30); label.setFont(new Font("Arial",Font.BOLD,20)); openFirstFrame=new JButton("Open First JFrame"); openFirstFrame.setBounds(100,200,300,30); openFirstFrame.setFocusable(false); frame.add(label); frame.add(openFirstFrame); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
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.
1 2 3 4 5 6 7 8 |
class SecondJFrame implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { } } |
- 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.
1 |
openFirstFrame.addActionListener(this); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
//importing all the necessary packages import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //Creating First Class for the First JFrame and implementing ActionListener interface public class FirstJFrame implements ActionListener { //Creating objects JFrame frame; JButton openSecondFrame; JLabel label; //Creating constructor FirstJFrame(){ //Setting properties of JFrame frame=new JFrame("First JFrame"); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.PINK); frame.setResizable(false); //Setting properties of JLabel label=new JLabel("This is First JFrame"); label.setBounds(140,100,200,30); label.setFont(new Font("Arial",Font.BOLD,20)); //Setting properties of JButton openSecondFrame=new JButton("Open Second JFrame"); openSecondFrame.setBounds(100,200,300,30); openSecondFrame.setFocusable(false); openSecondFrame.addActionListener(this);//Registering ActionListener to JButton //Adding components to JFrame frame.add(label); frame.add(openSecondFrame); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //Creating main method public static void main(String[] args){ //Creating object of the class FirstJFrame FirstJFrame firstJFrame=new FirstJFrame(); } //Overriding actionPerformed() abstract method @Override public void actionPerformed(ActionEvent e) { SecondJFrame secondJFrame =new SecondJFrame();//Opening the second JFrame frame.dispose();//Disposing the First JFrame } } //Creating Second Class for the Second JFrame and implementing ActionListener interface class SecondJFrame implements ActionListener{ //Creating objects JFrame frame; JLabel label; JButton openFirstFrame; //Creating constructor of the class SecondJFrame(){ //Setting properties of JFrame frame=new JFrame("Second JFrame"); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.CYAN); frame.setResizable(false); //Setting properties of JLabel label=new JLabel("This is Second JFrame"); label.setBounds(140,100,230,30); label.setFont(new Font("Arial",Font.BOLD,20)); //Setting properties of JButton openFirstFrame=new JButton("Open First JFrame"); openFirstFrame.setBounds(100,200,300,30); openFirstFrame.setFocusable(false); openFirstFrame.addActionListener(this);//Registering ActionListener to JButton //Adding components to JFrame frame.add(label); frame.add(openFirstFrame); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //Overriding the actionPerformed() abstract method @Override public void actionPerformed(ActionEvent e) { FirstJFrame firstJFrame=new FirstJFrame();//Opening the First JFrame frame.dispose();//Disposing the Second JFrame } } |
- Now run your program.
- Now click on the Button to Open the new JFrame.
- 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…
- Java Button Click Event
- How to Create Multi User Login Form in Java using MySQL Database
- How to Play Mp3 File in Java Tutorial | Simple Steps
- Number Guessing Game in Java Swing with Source Code
- Menu Driven Program in Java Using Switch Case
- Calculator Program in Java Swing/JFrame with Source Code
- Registration Form in Java With Database Connectivity
- Tic Tac Toe Game in Java with Source Code
- Text to Speech in Java
- How to Create Splash Screen in Java
- Best Laptops for Java Programming
- How to Connecty MySQL Database in Java Using Eclipse
- How to Connect MySQL Database in Java using NetBeans
- How to Fetch Data from Database in Java to JTable
- Why Pointer are not used in Java?