Building a Java GUI - Containers
Written by Mike James   
Article Index
Building a Java GUI - Containers
Adding A Button
Dialogs
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. 

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

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.

Containers

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 - but applets are no longer used and most browsers no longer support them.

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. As  already mentioned nearly all browsers have disabled support for this method of running Java and it makes sense to ignore it.

The JFrame Class

After introducing the types of container it seems worth looking in more detail at the frame. The reason is that alll containers 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. Next right click on the Source Packages folder and select New, Java Main Class - accept the defaults.

When the project has been created open the file NewMain.java. The code that you see contains the definition of a class called NewMain 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:

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

public class NewMain {
 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.