Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Lecture 8

Graphical User Interfaces

Read on for an overview of creating graphical user interfaces using the Java AWT library or the Java Swing library.  The first part describes the AWT class but you should note that we will be writing applets and applications using the Swing classes.

Graphical User Interfaces

AWT Library

Swing

Components

Containers

Layout Managers

Event Handling

 

 


Graphical User Interfaces

Most of the exercises you have tacked so far involve either output to a command prompt window or drawing simple graphic shapes to an applet window.  You need to learn how to create graphical user interfaces using Java.  

You can create applets or applications with menus, buttons, text fields and many other components.  Your applets and applications can respond to events such as mouse clicking etc. 

To accomplish all this you need to learn about the AWT (Abstract Windowing Toolkit) or alternatively about Swing.  

I will briefly describe the AWT first since most books and tutorials still focus on the AWT.  However, we will be using the Swing classes to create our graphical user interfaces in preference to the AWT.


AWT Library


Overview

The Java AWT contains all the tools that are required to build fully functional, windows-based graphical user interfaces.  The AWT is a general-purpose library which provides classes that encapsulate many useful graphical user interface (GUI), containers and components.  

Examples of components would be Buttons, Text Fields, Scroll Panes and Labels.

Components are held in containers.  Examples of containers would be Frames, Dialogs and Applets.  Menus may be considered to be components or containers.

Containers can hold other containers and containers and components within containers can be positioned using Layout Managers.

Finally, using the AWT requires an understanding of event-driven programming so that applications can respond to events such as mouse clicks.  This involves using the methods of the java.awt.Event class.

So, the AWT classes can be thought of as divided into 4 areas:

·       components 

·       containers 

·       layout managing 

·       event handling


Swing

Swing is the unofficial name for the JFC (Java Foundation Classes).  The JFC has been used since 1997 to help build graphical user interfaces.  Although the version of Java that we are using still supports the AWT classes, it is recommended that the JFC classes are used instead to build graphical user interfaces.

Have a look at the Swing class hierarchy below.

 

Here are some of the advantages of using Swing over the AWT.

  • Swing buttons and labels can display images as well as text.

  • You can easily add or change the borders drawn around most Swing components. 

  • You can easily change the behavior or appearance of a Swing component by either invoking methods on it or creating a subclass of it.

  • Swing components don't have to be rectangular. Buttons, for example, can be round.

  • Swing lets you specify which look and feel your program's GUI uses. By contrast, AWT components always have the look and feel of the native platform.

Like the AWT, Swing classes can be thought of as divided into 4 areas:

·       components 

·       containers 

·       layout managing 

·       event handling

The main difference to remember when using Swing components is to import the Swing package:-

import javax.swing.*;

Also, all Swing components and containers start with the letter J.  So for example, where we have an AWT Button, the Swing equivalent is JButton.  A Frame would be a JFrame and so on.

Most Swing programs also need to import the two main AWT packages:

import java.awt.*;
import java.awt.event.*;

Finally, if you want to use Swing components in an applet, you need to use JApplet.  Your class declaration would change from:-

public class AnApplet extends Applet {

to

public class AnApplet extends JApplet {

 

Note: --  it is better to either use the AWTcomponents and containers exclusively or the Swing containers and components exclusively.  Mixing AWT and Swing in the same applet or application is not recommended.

         

 


Components

The Component class is an abstract class which defines the elements common to all GUI components.  The components to be considered are:-

  • JLabel

an area where non-editable text can be displayed

  • JButton

an area that triggers an event when clicked

  • JTextField

an area in which the user inputs data from the keyboard

  • JTextArea

an area for manipulating multiple lines of text.

  • JChoice

provides a list of items from which the user can make a selection

  • JCheckBox

a state button that have on/off, true/false values

  • JRadioButton 

a group of buttons where only one button in the group can be true

  • JList

an area that triggers an event when clicked

 


Containers

A container is a component, which can contain other components.  Examples of containers would be JFrames, JDialogs and JApplets.  Containers help you organise placing of components.  You cannot use a component unless it is placed within a container.

JApplets, JDialogs and JFrames and JPanels are examples of top-level containers.  To be strict, a JApplet is a subclass of the Panel class.  So really a JApplet is a kind of Panel

  • JPanel

 A JPanel is a general purpose container.  It is not a window; it's sole purpose is to help you organise your components. 

  • JFrame

A fully functioning window which may contain a pull-down menu and have a border, a title and buttons for closing it.  Applications typically us a JFrame.
  • JDialog

A pop-up window generally used to display message to users.  Example: - "Are you sure you want to quit without saving?"

  • JApplet

A subclass of PanelJApplets are run by browsers.

Java does have other containers but we will concentrate on these.

 

Layout Managers

 


Layout Managers

Java has Layout Managers that help us position our components and containers.  We know that containers are used for placing components in.  However, when you add a component to a container, the default Layout Manager for that container positions that component for us.  However, components frequently need to be positioned in a way other than the default.  We can use Layout Managers to control the positioning of components in a container.  

Each separate container can be given a Layout Manager that decides how  the components are arranged within that container.  

Don't forget, containers can also be contained by other containers and they will be arranged according to the Layout Managers of the container they are in.

  •  Flow Layout

This is the simplest layout manager. It lays components out left to right, top to bottom.  It is the default layout manager for Panel

 

  • Border Layout

The default for all windows (when writing applications). It lays components according to a North, South, East, West and Centre

  • Grid Layout

Lays the components out in a grid with the number of rows and columns

  • Card Layout

A special kind of layout - it will display only one component at a time.

  • Grid Bag Layout

The most flexible layout manager -  also the most complex and difficult to use

 

 

Event Handling

 

 


Event Handling

Event handling is how programs respond to external events, such as the user pressing a mouse button. Swing programs perform all their painting and event handling in the event-dispatching thread.

 


That is folks!!

Now work through the Containers tutorial

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002