Saturday, April 13, 2019

How to Open Another Existing jFrame in Java NetBeans

How to Open Another Existing jFrame in Java NetBeans

Suppose, we have added two jFrame and want to open another jframe through existing jframe’s button’s click event. Suppose, First is ‘TestJFrame’ and second is ‘Second_Jframe’. Both contains same packages. So the point is, we want to open it through button’s press and back to first through second jframe’s button press.
Add_two_jFrame_in_Java
Add jFrame
Now, here SKOTechLearn will describe step by step process how to open another existing jframe in java netbeans.

How to use jLabel in java step by step


First we Design ‘TestJFrame’ through adding some controls like jLabel and jButton. And add some code inside button’s "ActionPreformed" event.
Code_for_open_Existing_Jframe
Code for Open Existing Jframe
As you can see that we have described the code description in above given image. You can copy code from following.
 private void MyButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
 Second_Jframe jfrm2= new Second_Jframe();
            jfrm2.setSize(270, 160); 
            jfrm2.setVisible(true);
            this.setVisible(false);
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
     this.dispose();
    } 

In ‘TestJFrame’, it is necessary to close window when the frame will be hide as:
 this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

After that when you press button to open another one, you have to dispose the existing form when another will opened.
 this.dispose();


How to auto resize controls and how to change control's name in java


You can set the size or leave it with existing default size as you designed. For size define set it like:
 jfrm2.setSize(270, 160); 

When you run application, it will show you the following way:
Jframe open output

Now if you want to back to your first frame, then you have to write code in button’s ‘ActionPerformed’ event as you write in first frame.

The code is same as:

private void MyButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
TestJFrame jfrm1= new TestJFrame();
             jfrm1.setSize(270, 160); 
             jfrm1.setVisible(true);
             this.setVisible(false);
             this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
             this.dispose();
    } 

The steps will be shown in bellow figure.
Open previous jframe process
Back to open Previous frame
Now follow the steps and copy this code and paste it in desire button’s event or any events from where you want to show.

How to set or display image in jframe in java


After doing all these process there will be the output present.
Open first and second jframe output
In this way you can show your static frame, which you have designed and want to show at run time with one click.

Open Dynamic JFrame:
For dynamic frame visibility, you can use the code as described beneath.

private void MyButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  JFrame Dynjfrm= new JFrame();
  Dynjfrm.setTitle("This is Dynamic jFrame");
  Dynjfrm.setLocation(400, 240);
            Dynjfrm.setSize(270, 160); 
            Dynjfrm.setVisible(true);
             this.setVisible(false);
             this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
      this.dispose();
  }   

In Dynamic process, you have to set the location, size etc through code as mention above.

Now you can understand how to show dynamic or static jframe in java Netbeans. I hope this post will helps you to implement the development process.

jRadioButton Style with Image in Java Swing


SKOTechLearn explains everything with details and deep way for better understanding. So, the process of How to Open another existing jframe in java netbeans has been completed.

Types of MessageBox in Java NetBeans with jOptionPane

0 comments: