Thursday, 10 November 2011

Java swing, navigate through JFrames passing data


Navigate through JFrames and pass data...

Now that we know how to create a new project and do button click events, let’s see how we can navigate through different JFrame and pass data along...

In this tutorial we will look at,

  • How to navigate between different JFrames
  • How to pass data between JFrams
  • Initialising data in JFrames


So to start this tutorial, make a new Java project, create a new package and add two JFrames.
For this project I will be using the same package I created before.

In my case I will name the first Frame as JFrame1 and the other one as JFrame2. Project with new frames should look like this.





Now, in one JFrame add a JTextFiled, a JButton and in the other JFrame add 2  JLabels. Rename the elements as shown in the images below
Don’t forget to add, JPannels to both of those JFrames before you add anything. It’s not a must, but its a  good practice :D

JFrame1 should look like this




JFrame1 element names and hierarchy (From the inspector window)
(Change variable names accordingly)









JFrame2 should look like this (note that there are 2 JLabels here)






JFrame2 element names and hierarchy should look like this
(Change variable names accordingly)





Now that we are done with the naming, let’s add some code.

In JFrame1 in the button click method, create object of the JFrame2 and make it visible and dispose the current frame. The code should look like this

private void jButtonGoToFrame2ActionPerformed(java.awt.event.ActionEvent evt) {
       
        //creates a new JFrame2 object ... and passes a string parameter to its constuctor ..
        JFrame2 jFrame2 = new JFrame2(jTextFieldEnterName.getText());

        //new jFrame2 object is set to visible mode
        jFrame2.setVisible(true);

        //JFrame1 object is disposed...
        this.dispose();
       
    }


In the above code, first a new object of the JFrame2 class is created. For the constructor of the JFrame2 class, a string value is passed (the JTextFieldEnterName variable’s string value). Of course JFrame2 does not have a constructor that takes a String YET. We have to create it later on.
 In the second line it sets the new jFrame2 object visible.
On the third line it disposes the current JFrame1, releasing all its resources.

Next we need to go to JFrame2 and create a constructor that takes in a String parameter.
Here is how to do it,

Go to the source of JFrame2 and just below its empty constructor (which is JFrame2() ), create a new constructor called JFrame2(String var).

Here’s how the code should look like, in this method, we will load these values to the JFrame2’s JLabel, just as the frame loads.

public JFrame2(String var){
        initComponents();
        jLabelValuePrinter.setText(var);
    }


This initComponents() method will initialise all the elements in the JFrame2, and it is essential.
In the second line, the value that is passed to the JFrame2 constructor is passed to the jLabelValuePrinter’s setText method. Therefore it will display the value passed in to the constructor.

Remember, you DON’T have to delete the constructor that was already there (the empty constructor). The Java compiler will find the relevant constructors accordingly.

(For further information about how the Java compiler identifies the correct constructor, read about method signatures in java.)

Now that your coding is complete save all the documents and run the JFrame1, enter some values and click the button. You will see that the values are passed in to the new window :D
















4 comments: