Using Frames, Components and Containers in Java

If you are using JCreator, Eclipse, Intellij Idea, Netbeans or any other IDE made for Java, creating a GUI application can be done in minutes by drag and dropping components from the toolbox without much effort. But most novices don’t have any idea about what happening on there behind the seen. We know the goal in application development is to create the best possible software in the least amount of the time. Frameworks and IDEs makes your work easier. But when in learning, actually it brings an exciting and enjoyable experience when your application is 100% done with the code made by your fingers rather than using third party tools. Let’s shift back to the simple text editor for a while! This simple guide is to build a graphical user interface application in a very basic way.

The below Java application code builds a simple form window by using the “swing” components in Java. Compile and run it to see how it works.

1
2
3
4
5
6
7
8
9
10
11
12
import javax.swing.* ;
 
public class JFrameWindow
{
public static void main(String[]args)
{
JFrame f=new JFrame ("JFrame Window");
f.setBounds(100,100,400,300);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.show();
}
}
import javax.swing.* ;

public class JFrameWindow
{
public static void main(String[]args)
{
JFrame f=new JFrame ("JFrame Window");
f.setBounds(100,100,400,300);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.show();
}
}

import javax.swing.*;

All graphical components we need to create application are integrated in this “swing” package. After importing that in our java application, we can use built in classes on there anywhere in our application code.

JFrame f=new JFrame (“JFrame Window”);

We have created the new object “f” by using the JFrame class on swing package. Then we have set the location and size of the frame by using setBounds method on JFrame and setDefaultCloseOperation for decide the exit functionality on the frame window (if you miss this, the window will not respond with close button). And finally we invoke the show() method on JFrame class to display the window.

And here how it looks like.

JFrame Window
Let’s see how to add components and containers to this frame. Components are various elements like buttons, text boxes, check boxes, labels and etc. Containers are uses for place the components as the designer preferences. Frames and panels are the examples for the containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.awt.*;
import javax.swing.*;
 
public class Flow extends JFrame
{
 
public Flow(String title)
{
super(title);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
 
public static void main(String [] args)
{
Flow f= new Flow("Flow Layout");
Container c=f.getContentPane();
c.setLayout(new FlowLayout());
 
for (int i=1; i<10; i++)
{
c.add(new JButton(""+i));
}
 
f.pack();
f.show();
}
}
import java.awt.*;
import javax.swing.*;

public class Flow extends JFrame
{

public Flow(String title)
{
super(title);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}

public static void main(String [] args)
{
Flow f= new Flow("Flow Layout");
Container c=f.getContentPane();
c.setLayout(new FlowLayout());

for (int i=1; i<10; i++)
{
c.add(new JButton(""+i));
}

f.pack();
f.show();
}
}

In above code we have extended the JFrame class to our class. Therefore we could access all members in JFrame class in our program’s public class “Flow”. In class constructor “public Flow(String title)” initializes the title of the JFrame and the default close operation. Let’s see the next procedures line by line.

Flow f= new Flow(“Flow Layout”);

We create object “f” by using the Frame class (inherited JFrame class).

Container c=f.getContentPane();

In here we have created a variable called “c” in Container type. And “c” has assigned to the f’s container. the c variable contains the f’s container now.

c.setLayout(new FlowLayout());

We set the c’s layout as flow layout. In flow layout all components in it behaves in same distance gap. Even whether the form resizes components inside it keep the same distance from each other components.

for (int i=1; i<10; i++)
{
c.add(new JButton(“”+i));
}

This is simply adding 9 buttons by using a for loop which runs 10 iterations. c.add(new JButton(“”+i)) code adds the button component to the c container by using the JButton class on swing package we imported.

f.pack();
f.show();

The pack() method on the “f” use to integrate the layouts and components in the Frame. And as the final step we view the form by calling the show method on the “f”.

This is what you see after compiling and running the code.

Flow Layout

Besides the Flow layout there are many other layouts such as Border layout, Grid layout and Box layout which arrange and organizes components in various styles.

As you noticed, still the buttons on this application doesn’t perform any action when you click on them. The components requires action listeners to do something on the program. We’ll meet again with a new post on how to add action listeners to designed components sooner.

Leave a Reply