Java Swings/Swings
First GUI
editOK, its now time to dive into swings. Fire up your text editor or IDE and type the code as shown below.
HelloSwing.java
import javax.swing.JFrame;
public class HelloSwing {
public static void main(String[] args) {
JFrame frame = new JFrame(); //We create a new frame
frame.setSize(200, 100); //Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Frame must close when close button is pressed
frame.setVisible(true);
}
}
|
Output
Compile the code and run it. If you get the output as shown, you have done it all right.
How it works?
editNow 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:
Code section
JFrame frame = new JFrame();
|
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:
Code section
frame.setSize(200, 100);
|
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:
Code section
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
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:
Code section
frame.setVisible(true);
|
So we have created our HelloSwing program.
Diving Into Swing
editFrame with Title
editIn 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 FrameWithTitle
. All the code is just the same as previous example with just one change. Notice the first statement in main method:
Code section
JFrame frame = new JFrame("Hello Swing");
|
Here the new JFrame() constructor gets a string argument. This argument forms the title of the frame. Simple!
FrameWithTitle.java
import javax.swing.JFrame;
public class FrameWithTitle {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
frame.setSize(200, 100); // Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.setVisible(true);
}
}
|
Output
Compiling the FrameWithTitle code will give an output as shown above.
Adding Labels
editWe 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.
AddingLabel.java
import javax.swing.JFrame;
import javax.swing.JLabel;
public class AddingLabel {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
frame.setSize(200, 100); // Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.add(new JLabel("Diving into swing!")); // Adding a label
frame.setVisible(true);
}
}
|
Output
Look at the line
Code section
frame.add(new JLabel("Diving into swing!"));
|
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 new JLabel("Diving into swing!")
creates a new label. Once added, the frame is displayed using frame.setVisible(true);
If all had been done right, you must be getting output as shown above.
Label and Text
editLabelnText.java
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LabelnText {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
frame.setSize(200, 100); // Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.add(new JLabel("Diving into swing!")); // Adding a label
frame.add(new JTextField("Type something here"));// Adding text field
frame.setVisible(true);
}
}
|
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 frame.add(new JTextField("Type something here"));
, 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
editLayoutManagerDemo.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LayoutManagerDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
frame.setSize(200, 100); // Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.setLayout(new FlowLayout()); // Adding layout, flow layout in this case
frame.add(new JLabel("Diving into swing!")); // Adding a label
frame.add(new JTextField("Type something here")); // Adding text field
frame.setVisible(true);
}
}
|
Output
Let's see what occurs if we change the frame dimension.
LayoutManagerDemo1.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LayoutManagerDemo1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
frame.setSize(500, 100); // Setting size of width=500px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.setLayout(new FlowLayout()); // Adding layout, flow layout in this case
frame.add(new JLabel("Diving into swing!")); // Adding a label
frame.add(new JTextField("Type something here")); // Adding text field
frame.setVisible(true);
}
}
|
Output
Packing Frames
editPacking optimize the size of the frame.
PackingFrames.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PackingFrames {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing"); // We create a new frame
//frame.setSize(500, 100); // Setting size of width=200px and height=100px
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame must close when closed button is presses
frame.setLayout(new FlowLayout());
frame.add(new JLabel("Diving into swing!")); // Adding a label
frame.add(new JTextField("Type something here")); // Adding text field
frame.pack(); // Packing components into frames. This is like auto sizing of frames
frame.setVisible(true);
}
}
|
Output