Java Swings/Swings

First GUI
OK, its now time to dive into swings. Fire up your text editor or IDE and type the code as shown below.

Output



Compile the code and run it. If you get the output as shown, you have done it all right.

How it works?
Now let's examine how it works. As in all Java programs, this program starts by creating a class, we have named this class HelloSwing. This class contains the main method from where the program starts execution. The first line in this function is:

In the first line we create new Java Frame or JFrame. A frame is nothing but a Swing container that holds other components like labels, buttons, text fields, menu bars etc. Now since we have created a frame and before displaying it, we need to set its size. To set the size of the frame we use the following code:

The setSize(int width, int height), receives two arguments, the first one is the frame width and the second one is the frame height. We have assigned the frame size to be 200 pixels (px) wide and 100px in height. So the frame dimensions has been set successfully.

Now what will happen if someone clicks the close button in the frame? We want the program to exit and clear off the memory when close button is clicked, hence to close the frame we use the function setDefaultCloseOperation. We want the program to exit when the close button is pressed and hence we use the following code:

All has been done right, all we need to do now is to make the frame visible. For that we use the setVisible(boolean b)  function. This function accepts one boolean input. If passed true, the frame is visible, else if passed false the frame is invisible. To make the frame visible, we use the following code:

So we have created our HelloSwing program.

Frame with Title
In the last example we saw how to create a frame that has nothing in it. This time we will create a frame that has a title. For that let's create a class. All the code is just the same as previous example with just one change. Notice the first statement in main method:

Here the new JFrame constructor gets a string argument. This argument forms the title of the frame. Simple!

Output



Compiling the FrameWithTitle code will give an output as shown above.

Adding Labels
We have created an empty frame, a frame with a title, now let us see how to add a simple component to a frame. Label is a simple component and we will add it to our frame. Read the program below carefully. It has a new statement when compared to the previous example.

Output



Look at the line

In this line we tell the computer to add a component or object to the frame using add function. Since we want to add a label we pass a new label object using new JLabel command.

The  creates a new label. Once added, the frame is displayed using

If all had been done right, you must be getting output as shown above.

Label and Text
Output



In the last example (AddingLabel.java) we have seen how to add a JLabel to a JFrame. Now we will increase our knowledge by adding more components. Lets add a text field (that's JTextField) along with label. The program shown above (LabelnText.java) has the same code of AddingLabel.java but with a minor difference. Look at the penultimate line, in this line we have added a JTextField to frame using the add method. To the add method we pass a new JTextField using new JTextField("Type something here") statement. The JTextField constructor can accept a string argument which is set as text value of the text field.

So when we run the example we would expect the frame to have a label and a text field at the right of it. But look at the output. What's wrong? All we see is a text field that occupies the entire frame! Java has a bug? The answer is both label and text fields are present in the frame. The text field is drawn on top of the label.

Java Virtual Machine is dumb. You told to put an label, so it did put one onto the frame, next you told to put a text field so it did faithfully. But it put the text field on top of the label. To get a desired output like a label and text field laid next to each other we must use what is called a Layout Manager which you will be briefed about shortly.

Layout Manager
Output



Let's see what occurs if we change the frame dimension.

Output



Packing Frames
Packing optimize the size of the frame.

Output