So you started learning java and someone told you to use NetBeans IDE as it will make your work easier by providing a easy-to-use drag & drop GUI builder. Yes, it does. It really makes programming easy for a beginner by removing all the coding required for the designing part. But problems crops up when you go advanced. And when you search the web for help, you would really find it difficult to understand other’s answers.
One such problem for which it would be difficult to get answer for, is passing values from one JFrame form to another.
So, here’s is a simple guide to help you move forward.
To know how to pass a value/variable from one form to another, you need to know ‘What is a Constructor?’.
Constructor
♦ A Constructor is like method which has the same name as it’s class.
♦ It is used to initialize the objects of that class type with a legal initial value.
♦ When you create a object of a class, the compiler automatically calls it’s constructor.
♦ Constructor has no return value, not even void.
So, we would have to overload the class’s constructor in which we want to get the value. I have designed two simple JFrame forms, the first is a login screen in which a user would enter his username and password and second is a welcome screen in which we will get his username from the previous screen and display it in a label.
Here’s the design view of the both the forms.
Double click the Log in button to open it’s actionPerformed event and paste the following code:-
String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
if (user.equals(“admin“) && pwd.equals(“pass“))
new Welcome(user).setVisible(true);else {JOptionPane.showMessageDialog(this, “Incorrect Username or Password!”);}
As soon as you paste the code, you will see a error and NetBeans will ask you to create a constructor Welcome(java.lang.String), ignore this warning for now.
As you can see, we are passing the variable user(which stores are username i.e admin) in the constructor for the Welcome form. Now, open the source tab in your Welcome form and find it’s default constructor and paste/type the following code below it:-
public Welcome(String User) {username = User;
initComponents();}
Now, declare String variable username just below your class declaration. Your code should now look like this-
public class Welcome extends javax.swing.JFrame {String username;
/*** Creates new form Welcome*///Default constructorpublic Welcome() {initComponents();}//Our new constructorpublic Welcome(String User) {username = User;initComponents();}
Now, right click your welcome form and go to>Events>Window>windowOpened and use the username variable set it wherever you want!
Example:-
userTF.setText(username);
Now, go back to login form and press Shift + F6 to run your file. Enter your username and password and click login to see your name on the next screen.
You might be thinking now that why have two constructor, this is because netbeans let’s us to run a single JFrame Form so if delete the old the constructor then the Runnable which is placed at the end of your code will cause you errors.
Not just a simple string, you can pass any variable from one form to another and ofcourse you can pass a value further that you received from the previous class. So, let’s move onto another example.
Now from the welcome screen we will take an int value i.e age and a list of hobbies and pass on these values including username to a new screen. As you know we can initialize variables in the constructor, so I have declared an ArrayList under class declaration and initialized it in the constructor so that we can use it in any event we want. Here’s what code looks like now-
Welcome.java (only relevant part)
public class Welcome extends javax.swing.JFrame {String username;
ArrayList hobbies;/**
* Creates new form Welcome
*/
//Default constructor
public Welcome() {
initComponents();
}//Our new constructor
public Welcome(String User) {
username = User;
hobbies = new ArrayList();
initComponents();
}
In details form we will initialize the 3 variables that has been passed through the constructor. We will show username and age in labels and hobbies jList component.
Details.java
package demoforblog;import java.util.ArrayList;import javax.swing.DefaultListModel;/**** @author Rohan*/public class Details extends javax.swing.JFrame {int age;
ArrayList hobbies;
String user;
/**
* Creates new form Details
*/
public Details() {
initComponents();
}
public Details(int AGE, ArrayList HOBBIES, String USER) {
age = AGE;
hobbies = HOBBIES;
user = USER;
initComponents();
}private void formWindowOpened(java.awt.event.WindowEvent evt) {System.out.println(“”+age);System.out.println(“”+hobbies.get(0));jLabel6.setText(user);jLabel7.setText(“”+age);DefaultListModel list = new DefaultListModel();for (int i =0; i < hobbies.size(); i++ ) {list.addElement(hobbies.get(i));}jList1.setModel(list);}
Note:- >You have guessed it right the back button does nothing.
>System.out statements are only for testing, you can ignore them.
Now run your login form again to see all the screens in action.
I have used different colors, so that you don’t get confused with the variables, but if you still have any problem regarding it then the image below could help you.