Building a Java GUI - Containers
Written by Ian Elliot   
Wednesday, 07 November 2012
Article Index
Building a Java GUI - Containers
Using Swing GUI Builder
Obtaining User Input

In this chapter of Modern Java we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 


  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The User Interface - More Swing
  6. Working With Class
  7. Class Inheritance

We have already looked at the basics of using Swing in but the story isn't over yet. Using Swing to build a user interface is not only an essential skill, it also is a great way to meet other aspects of Java. In this chapter we look at how to create a more complicated Java project that can make use of more than one window to interact with the user. This takes us to the subject of containers and a more natural way to build a Java project.

So let's find out what containers are.


We have already discovered Swing components like the button and so on and we have made use of them via the drag-and-drop editor. If you need to brush up on the use of components then have a look at Chapter 2 and Chapter 5.

However we ignored the small matter of what exactly it was that the components where being dropped onto. Swing, and most other similar frameworks have a range of objects which can be used as containers for other objects like buttons and textboxes and so on. When you first start to use Swing you can take the container for granted but later you really need to understand what the container is doing, what containers are available and how to make use of them.

There are three container types - frames, dialogs and applets.

We also have the panel and a range of variations based on it but this isn't a true container- it's really a component and hence very limited type of container. All it does is allow you to group components together within other containers. In short, panels are fairly boring but occasionally useful when you want to group event handling together.

A frame on the other hand is a complete window. When you create a frame you get a fully resizeable movable windows separate from the browser’s window. You can add other components to a frame and you can add a menu. In all respects a frame is very much a fully fledged Window’s window but without the fuss.

A dialog is a specialised sort of window intended to hold nothing but components that the user can interact with. A dialog appears, the user clicks buttons, enters text and then it goes away. This is generally refered to as a "modal" dialog box but dialogs don't have to be model - however they are "owned" by other frames and they are deleted when their parent frame is closed.

The third type of continer - the applet - allows a Java program to run in a browser. This is something we need to devote a whole chapter to so let's take a look at the frame and the dialog.

The JFrame Class

After introducing the three types of container it seems worth looking in more detail at the frame. The reason is that the other two are similar in their use - but they have their own particular problems.  We have already encountered the frame in earlier chapters as it is the container that we have been using by default with the drag-and-drop editor.

Now that we know about classes and objects let's do the same job but this time without the help of the editor. A frame is represented by the JFrame class and to make frames or windows appear on the screen all we have to do is create an instance of the class i.e. a JFrame object.

To create a JFrame instance you simply use the standard way of creating an object from its class i.e:

JFrame MyFrame=new JFrame("My New Frame");

The text specifies the title used for the window that appears.

The new object MyFrame has a range of methods and properties that allow you to control the way it looks and how it behaves. 

So to try this out start NetBeans and create a new Java project called container - accept all of the defaults. When the project has been created open the file The code that you see contains the definition of a class called Container complete with the start of a single method called main - this is where your program starts. When you run a probject the class of the same name is used to supply the main method which is obeyed.

If you just run the project nothing much happens because there is no code in main. Let's add a frame to the main method and display it. To create the frame we simply use:

JFrame MyFrame=new JFrame("My New Frame");

The text that we pass to the constructor is used as a title.

After this we need to customize the JFrame object:

MyFrame.setSize(400, 400);
MyFrame.setLayout(new FlowLayout());
MyFrame.setVisible(true) ;

The first instruction sets the frame to 400 x 400 pixels, tells it to use a FlowLayout and then tells it to display. Don't worry about the setLayout method and the FlowLayout we need to look at these in more detail later.

If you just add these lines to main you will discover that NetBeans complains at you - there are errors!

When ever you make use of a class from Swing, or any other framework we have to add an import instruction to the start of the program so that the Java system can find it.

In this case we are using two classes - JFrame and FlowLayout - so we need to add:

import java.awt.FlowLayout;
import javax.swing.JFrame;

In fact you don't need to do it manually as if you click on the error symbol NetBeans will offer to add the necessary import instruction for you. 

With this change the entire program is:

package container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

 * @author Ian
public class Container {
     * @param args the command line arguments
 public static void main(String[] args) {
    JFrame MyFrame=new JFrame("My New Frame");
    MyFrame.setSize(400, 400);
    MyFrame.setLayout(new FlowLayout());
    MyFrame.setVisible(true) ;


Now if you run the program you will see a window appear with the title My New Frame.

So far so good but what about adding a button?

This is very easy.

All you have to do is create an instance of the JButton class and add it to the frame:

JButton MyButton=new JButton("Click me!");

The add method, as its name suggests, adds any component to the container.

This is the programmatic way of dropping a control onto the design surface in the designer. Of course it ignores the small problem of where the button will display within the container and this is what the layout manger mentioned eariler is responsible for. For the moment just accept the fact that the layout manager will take care of creating a default layour for your components. The drag-and-drop designer provides a much simpler way of working with layout managers.

You also have to remember to add an import instruction:

import javax.swing.JButton;

and run the result you will see a new window appear complete with button. 




You can resize the window and move it around the screen and the button will appear to work. If you were to define an event handler then it would do something when the button was clicked but it turns out to be much easier to do this and the intial construction of the frame using the drag-and-drop designer.

Last Updated ( Wednesday, 07 November 2012 )

RSS feed of all content
I Programmer - full contents
Copyright © 2014 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.