You are on page 1of 58

GUI-Based Event-Driven Programming

Event. The users interaction with the GUI


component.
Event-handling Methods. Methods that are
called when an event occurs.
The user interacts with a GUI component, the
program is notified of the event and the
program processes the event.

public class Frame extends JFrame implements ActionListener

A class can inherit existing attributes and


behaviors from another class via extends, and can
implement one or more interfaces.
In the class definition above, JFrame class will be
inherited and interface ActionListener will be
implemented.

Class JFrame
javax.swing

is

from

the

package

JFrame provides the basic attributes and


behaviors of a window a title bar and
buttons to minimize, maximize and close
the window.

Interface. Specifies one or more behaviors that you


must define in your class definition.
This interface specifies that this class must define a
method with the heading:
public void actionPerformed (ActionEvent e)
{
}
The methods task is to process a users interaction with
a GUI component that generates an action event.
The method is called automatically in response to the
users interaction.

Sets the windows size.

Syntax :
setSize (width, height);

By default a window is NOT displayed on the


screen, it can be displayed using the
setVisible method.

Syntax :

setVisible (true);

Displays the text in the title bar of the


window.

Syntax :

setTitle (text on title bar);

Displays the text in the title bar of the


window.
Should be the
constructor.

first

statement

Syntax :

super (text on title bar);

of

the

When a window is no longer needed in an


application , it must be explicitly disposed of.
This can be done by explicitly calling the
method setDefaultCloseOperation with the
argument JFrame.EXIT_ON_CLOSE

From package java.awt


Use to attach a GUI component on the
content pane so they can be displayed at
execution time

Syntax:
Container <object> = getContentPane ( );

getContentPane().A method that returns a


reference to the content pane that can be
used to attach GUI component

Used to define the layout manager for the applets


user interface.
Layout Manager.
Provided to arrange GUI
components on the Container for presentation
purposes. This determines the position and size of
every GUI component attached to the container.

Syntax:

Object of Container.setLayout (new FlowLayout ( ) );

The most basic layout manager


GUI components are placed on the container
from left to right in the order in which they
are attached to the container via the method
add.
When the edge of the container is reached,
components are continued to the next line.

Contains a string of character to display on the


screen.
Normally indicates the purpose of another GUI
component on the screen.

Syntax:
JLabel object_name;
// declaration
object_name = new JLabel (String to display);

JLabel label1;
label1 = new JLabel (Enter a value);

To attach this to the content pane, use add method:

Syntax:
Object of Container. add(object of JLabel);
Example, if c is the content pane, to add the new JLabel
label1, use:
c.add (label1);

Used to get a single line of information from the


user at the keyboard or display information on
screen.

Syntax:
JTextField object_name;
// declaration
object_name = new JTextField (size);

JTextField field1;
field1 = new JTextField (10);

To attach this to the content pane, use add method:

Syntax:
Object of Container. add(object of JTextField);
Example, if c is the content pane, to add the new
JTextField field1, use:
c.add (field1);

Used to determine if user can edit entries in the


JTextField.
false value means the user cannot edit entries in
the JTextField (gray background), a true value
means user can edit entries (white background)

Syntax:
JTextField_object.setEditable (true/false);
Example:

field1.setEditable (false);

Rectangular object that the user can press to


perform an action

Syntax:
JButton object_name;
// declaration
object_name = new JButton(Text on Button);

JButton button1;
button1 = new JButton (Click here);

To attach this to the content pane, use add method:

Syntax:
Object of Container. add(object of JButton);
Example, if c is the content pane, to add the new
JButton button1, use:

c.add (button1);

Specifies that the program should listen for events

The keyword this enables the program to refer to


itself

When the user interacts with a GUI component an


event is sent to the program

Example:

button1.addActionListener (this);

When the user press the button, an event is sent to the applet
indicating that an action was performed by the user and calls
the method actionPerformed ( ) to process user interaction.

Used to specify the tool tip that is displayed


automatically when the user positions the mouse
cursor over the label in the GUI

Syntax :
Object of GUI .setToolTipText(Text);
Example:

labek1.setToolTipText("DLSU - Dasmarias");

Used to get input from the JTextField

Syntax:

Example:

object of JTextField.getText()

JTextField field1;
int num;
num = Integer.parseInt (field1.getText ( ));
Note:
Any input from a JTextField is a String.

Used to display string of information on the


JTextField

Syntax:
object of JTextField.setText (String to display);
Example:
Note:

field1.setText (De La Salle);

JTextField can only display String.

Used to convert integer values to string.

JTextField can ONLY display Strings.

Example:
num = 10;
field1.setText(Integer.toString (num));

Used to convert numeric values to string.

Example:
num = 10;
field1.setText(String.valueOf (num));

From java.awt
Used to define the background color and
foreground color that will be used by the GUI
component.

Defines the background color of the Container


and some GUI components.

Syntax 1:
object.setBackground(Color.predefined color);
Examples:

c.setBackground(Color.orange);
field1.setBackground(Color.green);

black
blue
cyan
darkGray
gray
green
lightGray

magenta
pink
orange
red
white
yellow

Syntax 2:
object.setBackground(new Color (R,G,B));
Examples:
c.setBackground(new Color(0,128,64));
field1.setBackground(new Color(0,128,64));
Note:
RGB values are the RED, GREEN and BLUE
components of a Color (values are from 0 to 255)

Defines the
components.

foreground

color

of

the

GUI

Syntax 1:
object.setForeground(Color.predefined color);
Examples:

c. setForeground(Color.yellow);
field1. setForeground(Color.blue);

Syntax 2:
object.setForeground(new Color (R,G,B));
Examples:
c.setForeground(new Color(0,128,64));
field1.setForeground(new Color(0,128,64));
Note:

0,0,0 is black
255, 255, 255 is white

From java.awt.

Use to define the font name, font style and font size of the text
that will be used by a GUI component.

Syntax:
Font <object> = new Font ( "Font Name", Font Style, Font Size);

Example:
Font font = new Font ("Trebuchet MS", Font.BOLD, 24);
Note:
Available Font Style are: Font.BOLD, Font.ITALIC, Font.PLAIN

Font.BOLD

Font.ITALIC 2

Font.PLAIN

Font.BOLD + Font.ITALIC 3

Use to set the Font defined by the Font class.

Syntax:
Object of GUI.setFont(object of Font Class);
Example:
Font font = new Font ("Trebuchet MS", Font.BOLD, 24);

Label1.setFont(font);

import
import
import
import

javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JTextField;
javax.swing.JButton;

import
import
import
import

java.awt.FlowLayout;
java.awt.Container;
java.awt.Font;
java.awt.Color;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

Package: javax.swing
Consists of a label positioned beside a circle;
you can click the cirlce to select or deselect
Syntax 1:

//no label, unselected

Example:

Syntax 2:

Example:

Syntax 3:

//with label, selected

Example:

Package: javax.swing
Consists of a label positioned beside a
square; you can click the square to display or
remove check mark
Syntax 1:
JCheckBox object= new JCheckBox();

//no label, unselected

Example:

Syntax 2:

Example:

Syntax 3:

//with label, selected

Example:

JCheckBox and JRadioButton have the same


set of properties and methods.
Methods and properties that can be applied
to JRadioButton object can also be applied to
JCheckBox object.

Package: javax.swing
When the status of a JCheckBox changes from
unchecked to checked( or from checked to
unchecked), an ItemEvent is generated and
the itemStateChanged() method executes.

sets the state of the JCheckBox to true for


selected or false for unselected
Syntax:
object.setSelected(boolean);
Example:

Gets the current state (checked or unchecked


of the JCheckBox

Determines which object generated the event

Determines whether the event was a selection


or a deselection
Returns an integer that is equal to two class
variables:
ItemEvent.SELECTED
ItemEvent.DESELECTED

Package: javax.swing
Used to groups several components, such as
JCheckBoxes, so a user can select only one at
a time.
When you groupd JCheckBox objects, all of
the other JCheckBoxes are automatically
turned off when the user selects any one
checkbox.

Syntax:
ButtonGroup object = new ButtonGroup();
GUI_object.add(buttonGroup_object);
Example:

Method of JList which is used to determine the number of


items that are visible in the list.

Example:
colorList.setVisibleRowCount(5);

5 rows are initially displayed

This method of JList specifies the list selection mode.


Needs:
import javax.swing.ListSelectionModel;

Possible values are:

1)
2)
3)

SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELCTION

Example:
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

From javax.swing:
import javax.swing.JScrollPane;

Object that is used to provide the automatic


scrolling functionality for the JList.

Example:
c.add(new JScrollPane(colorList));

This is used to register an event that


implements a ListSelectionListener.
Calls
the
valueChanged
method
automatically to process the event.

Example:

colorList.addListSelectionListener(this);

Returns the selected item;s index in the list.

public void valueChanged (ListSelectionEvent e)


{
if (colorList.getSelectedIndex() == 0)
}

c.setBackground(Color.blue);
-OR-

Color color[]= {Color.blue, Color.cyan, Color.green, Color.magenta};


public void valueChanged (ListSelectionEvent e)
{

c.setBackground(color[colorList.getSelectedIndex()]);
}

setListData(). Sets the items displayed in a JList.

getSelectedValues(). Returns the selected items as an


array of Objects

Example:
public void actionPerformed(ActionEvent e)
{
list2.setListData(list1.getSelectedValues());
}

From java.awt
Import java.awt.Toolkit;

Example:
setIconImage(Toolkit.getDefaultToolkit().getImage(lasalle.jpg"));

You might also like