You are on page 1of 37

CT038-3-2

Object Oriented Development with


Java

Java GUI
Topic & Structure of the lesson

GUI
Event Listener
Applet

CT038-3-2 OODJ Java GUI


Learning outcomes
At the end of this lecture you should be
able to:
To understand the Java event model
To install event listeners
To accept input from buttons and text
fields

CT038-3-2 OODJ Java GUI


Key terms you must be able to use

If you have mastered this topic, you should


be able to use the following terms correctly
in your assignments and exams:
GUI
Event listener
Applet

CT038-3-2 OODJ Java GUI


Introduction

A graphical user interface (GUI) presents a


user friendly mechanism for interacting
with an application.

CT038-3-2 OODJ Java GUI


GUI Components
GUIs are built from GUI components.
It is an object with which the user interacts
via a mouse or the keyboard.
Some example of GUI components:
Menu bar
Buttons
Combo box
Scroll bar

CT038-3-2 OODJ Java GUI


Events
User interface events include key presses,
mouse moves, button clicks and menu
selection.
Every program must indicate which events
it needs to receive.
It does that by installing event listener
objects.
An event listener belongs to a class
defined by the programmer.

CT038-3-2 OODJ Java GUI


Its methods describe the actions to be
taken when an event occurs.
To install a listener, you need to know the
event source.
The event source is the user interface
component that generates a particular
event.
You add an event listener object to the
appropriate event source.

CT038-3-2 OODJ Java GUI


Event listener

Notified when event happens


Belongs to a class that is provided by the
application programmer
Its methods describe the actions to be
taken when an event occurs
A program indicates which events it needs
to receive by installing event listener
objects
CT038-3-2 OODJ Java GUI
Event source

Event sources report on events


When an event occurs, the event source
notifies all event listeners

CT038-3-2 OODJ Java GUI


Example

Use JButton components for buttons; attach an


ActionListener to each button
ActionListener interface: public interface
ActionListener
{
void actionPerformed(ActionEvent event);
}
Need to supply a class whose actionPerformed
method contains instructions to be executed when
button is clicked

CT038-3-2 OODJ Java GUI


event parameter contains details about the
event, such as the time at which it occurred
Construct an object of the listener and add it
to the button:
ActionListener listener = new ClickListener();
button.addActionListener(listener);
Whenever the button is clicked, it calls
listener.actionPerformed(event);
As a result, the message is printed.

CT038-3-2 OODJ Java GUI


File ClickListener.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// An action listener that prints a message.
public class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println(I was clicked.);
}
}

CT038-3-2 OODJ Java GUI


File ButtonTester.java
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
This program demonstrates how to install an action listener.
*/
public class ButtonTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);

ActionListener listener = new ClickListener();


button.addActionListener(listener);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static final int FRAME_WIDTH = 100;


private static final int FRAME_HEIGHT = 60;
}

CT038-3-2 OODJ Java GUI


Review Questions

Which objects are the event source and the


event listener in the ButtonTester
program?
Why is it legal to assign a ClickListener
object to a variable of type
ActionListener?

CT038-3-2 OODJ Java GUI


Answers

The button object is the event source.


The listener object is the event
listener.
The ClickListener class implements
the ActionListener interface.

CT038-3-2 OODJ Java GUI


Building Applications with Buttons

Example:
Lets study a simple investment viewer
program that puts a button to work.
Whenever the button is clicked, interest is
added to a bank account, and the new
balance is displayed.

CT038-3-2 OODJ Java GUI


First we construct an object of the JButton
class.
Pass the button label to the constructor:
JButton button = new JButton(Add interest);
We also need a user interface component
that displays a message.
Pass the initial message string to the label
JLabel constructor.

CT038-3-2 OODJ Java GUI


JLabel label = new JLabel(balance= +
account.getbalance());
The application frame contains both
button and the label.
So we cannot add both components
directly to the frame.
The solution is to put them into a panel.

CT038-3-2 OODJ Java GUI


JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
Next define a class that implements
ActionListener interface.
Place the button action into
actionPerformed method.

CT038-3-2 OODJ Java GUI


Listener class adds interest and displays the
new balance:
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double interest = account.getBalance() *
INTEREST_RATE / 100;
account.deposit(interest);
label.setText("balance=" +
account.getBalance());
}
}

CT038-3-2 OODJ Java GUI


Processing Text Input

Use JTextField components to provide


space for user input.
When a text field is constructed, you need to
supply the width.
final int FIELD_WIDTH = 10; // In characters
final JTextField rateField = new JTextField(FIELD_WIDTH);

Place a JLabel next to each text field.


JLabel rateLabel = new JLabel("Interest Rate: ");

CT038-3-2 OODJ Java GUI


Supply a button that the user can press to
indicate that the input is ready for
processing.
The button's actionPerformed method
reads the user input from the text fields
(use getText).

CT038-3-2 OODJ Java GUI


class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double rate=Double.parseDouble(rateField.getText());
. . .
}
}

CT038-3-2 OODJ Java GUI


Applet

Applets are small Java programs that are


primarily used in Internet computing.
They can be transported over the Internet
and run using the Applet Viewer or any
Java enabled web browser.

CT038-3-2 OODJ Java GUI


How Applets differ from Applications

Applets do not use the main() method.


Applets cannot run independently.
Applets cannot read or write to the files in
the local computer.
Applets cannot communicate with other
servers on the network.

CT038-3-2 OODJ Java GUI


Preparing to write Applets

We will need to know


When to use applets,
How an applet works,
What features an applet has

CT038-3-2 OODJ Java GUI


Steps involved in developing and
testing an applet are:
1.Building an applet code (.java file)
2.Creating an executable applet (.class file)
3.Designing a web page using HTML tags
4.Preparing <APPLET> tag
5.Incorporating <APPLET> tag into the web
page.
6.Creating HTML file
7.Testing the applet code.

CT038-3-2 OODJ Java GUI


Building Applet Code

It is important applet code uses two


classes :- Applet and Graphics classes.
Applet class which is contained in the
java.applet package provides life and
behavior to the applet.
The paint( ) method of Applet class,
displays the result of the applet code on
the screen.

CT038-3-2 OODJ Java GUI


The paint( ) method, which requires a
Graphics object as an argument, is
defined as:
public void paint( Graphics g)

CT038-3-2 OODJ Java GUI


Example: HelloJava.java
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint (Graphics g)
{
g.drawString(Hello Java,10,100);
}
}

CT038-3-2 OODJ Java GUI


Adding an Applet to HTML file

Insert the <APPLET> tag in the page at


the place where the output of the applet
must appear.

CT038-3-2 OODJ Java GUI


Example: HelloJava.html
<HTML>
<HEAD>
<TITLE>
Welcome to Java Applets
</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1> Welcome to the world of applets </H1>
</CENTER>
<BR>
<CENTER>
<APPLET
CODE = HelloJava.class
WIDTH = 400
HEIGHT = 200>
</APPLET>
</CENTER>
</BODY>
</HTML>
CT038-3-2 OODJ Java GUI
Syntax of <APPLET> tag
<APPLET
[ CODEBASE = codebase_URL ]
CODE = AppletFileName.class
[ ALT = alternate_text ]
[ NAME = applet_instance_name ]
WIDTH = pixels
HEIGHT = pixels
[ ALIGN = alignment ]
[ VSPACE = pixels ]
[ HSPACE = pixels ]
>
[ < PARAM NAME = name1 VALUE1> ]
[ < PARAM NAME = name2 VALUE2> ]
.
</APPLET>

CT038-3-2 OODJ Java GUI


Passing Parameters to Applets
Each <PARAM> tag has a name
attribute such as color and a value.
Example:
<APPLET
<PARAM NAME = color VALUE = red>
<PARAM NAME = text VALUE = Hello JAVA>

</APPLET>

CT038-3-2 OODJ Java GUI


Parameters are passed to an applet when
is loaded.
Define the init( ) method in the applet.
This is done using the getParameter( ).

CT038-3-2 OODJ Java GUI


Example: Applet HelloJavaParam
import java.awt.*;
import java.applet.*;

public class HelloJavaParam extends Applet


{
String str;
public void init ( )
{
str = getParameter(string);
..

}
public void paint (Graphics g)
{
g.drawString(Hello Java,10,100);
}
}

CT038-3-2 OODJ Java GUI

You might also like