You are on page 1of 11

ICT 314 ADVANCED JAVA TECHNOLOGY ICT 314 ADVANCED JAVA TECHNOLOGY

Instructor: Instructor: r d appiah r d appiah


Introduction: Building User Interfaces
In the earlier versions of J ava, the building of graphical
user interfaces is usually achieved through the use of y g
a set of classes called the Abstract Windowing Toolkit
(AWT)
In this course, we will learn the use of the javax.swing
package to create and design user interfaces.
This will be followed by the skills to arrange all of the
components on a user interface components on a user interface
And finally, learning to make user interface(s)
responsi e to ser control(s)
RDAppiah
responsive to user control(s).
The Abstract Windowing Toolkit
Thi i t f l th t bl t This is a set of classes that enables you to
create a graphical user interface and receive
i t f th d k b d user input from the mouse and keyboard.
Because J ava is a platform-independent
l th AWT ff t d i language, the AWT offers a way to design an
interface that will have the same general
d f ti lit ll t it appearance and functionality on all systems it
runs on.
However, it is always advised that developers
must test their AWT-based interfaces on as
RDAppiah
many platforms and browsers as possible.
The components of a GUI:
A i t f t d b d GUI i t An interface created based on GUI may consist
of three basic things:
Components: Anything that can be put onto a
user interface, including clickable buttons,
scrolling lists, pop-up menus, check boxes, and
text fields.
Containers: A component that can contain
other components These may include such other components. These may include such
things as an Applet window, Panels, Dialog
boxes and other standalone windows
RDAppiah
boxes, and other standalone windows.
Layout Managers: These are objects that
define howthe components in a container will define how the components in a container will
be arranged. Normally, one does not see a
layout manager in an interface but rather the layout manager in an interface, but rather the
results of its action(s).
The AWTs classes are all available within the
java awt * package java.awt. package.
Thus, to make all of its resources available in a
programme one has to add the follo ing import programme, one has to add the following import
statement at the top of the source codes:
import java awt *;
RDAppiah
import java.awt.*;
The user-interface Components:
Components are placed onto a user interface
by adding them to a container. y g
Note also that a container is itself a component
and can be added to other containers thus a
container within a container.
To a higher extent, we will use both the
java awt * and javax swing * packages to design java.awt.* and javax.swing.* packages to design
user interfaces.
RDAppiah
Steps:
Acomponent is added to a container via the A component is added to a container via the
following two steps:
C t th t f i t t d Create the component of interest and
Call the containers add( ) method with the
t component.
RDAppiah
GUI I/O WITH JOptionPane
The J OptionPane class is used to provide The J OptionPane class is used to provide
simple GUI-based input and output interfaces.
It is important to note that the J OptionPane
class support only a string input.
Thus, to input a numerical value, we need to
performthe string conversion ourselves perform the string conversion ourselves.
To achieve this, J ava provides a feature called
wrapper classes and their corresponding
conversion methods, as follows:
RDAppiah
Common wrapper classes and their
conversion methods: conversion methods:
Class Method Example
Integer parseInt Integer.parseInt(45)
>45
Long parseLong Long.parseLong(45)
> 45L >45L
Float parseFloat Float.parseFloat(45.8)
> 45 8F >45.8F
Double parseDouble Double.parseDouble(45
) 45 0
RDAppiah
)>45.0
Customizing Frame Windows
Creating a customized user interface involves Creating a customized user interface involves
defining a subclass of the JFrame class.
The J Frame class contains the most
rudimentary functionalities to support features
found in any frame window, such as
minimizing, moving and resizing the window.
For practical programming reasons, we
normally define a subclass of the J Frame class normally define a subclass of the J Frame class
and add methods and data members to
implement the needed functionalities
RDAppiah
implement the needed functionalities.
Illustration:
Refer to Ex6DefaultJ Frame java Refer to Ex6DefaultJ Frame.java
To define a subclass of another class, we
declare the subclass with the reserved word
extends.
To illustrate this, refer to Ex7JFrameSubclass.java
Notice that we declared the variable
contentPane in the changeBackColor() method g ()
as a Container. We do not have a class called
ContentPane.
RDAppiah