You are on page 1of 20

1

GUI
GUI package and classes
GUI components and API
GUI interpeted by Asaminew G. 01/27/2023
2

GUI package
• GUI package
• Is a directory that contain all class libraries of GUI component

• Two types: namely java.awt and javax.swing

• Components in the java.awt package are platform dependent i.e. different appearance and
way of user interaction which is called look-and-feel in different platform
• AWT components are tied into local platform; so they are known as heavyweight

• Components of javax.swing package are platform independent i.e. a uniform look-and-feel

• Some of Swing components are not tied into local platform; so they are known as
lightweight. Also there are heavyweight swing component which require direct interaction
with current operating system.

GUI : interpreted by Asaminew G. 01/27/2023


3

GUI classes
• Super classes of swing lightweight GUI component

Object

Component

Container

Jcomponent

GUI : interpreted by Asaminew G. 01/27/2023


4

Cont. …
• …

GUI : interpreted by Asaminew G. 01/27/2023


5

Cont. …
• Class Component (package java.awt) is a subclass of Object that declares many of
the attributes and behaviors common to the GUI components in packages java.awt
and javax.swing. Most GUI components extend class Component directly or
indirectly.

• Class Container (package java.awt) is a subclass of Component. Components are


attached to Containers (such as windows) so the Components can be organized and
displayed on the screen. Any object that is a Container can be used to organize
other Components in a GUI. Because a Container is a Component, you can attach
Containers to other Containers to help organize a GUI
GUI : interpreted by Asaminew G. 01/27/2023
6

Cont. …
• Jcomponent: The base class for all lightweight Swing components except top-level
containers (notice it is an abstract class)
• some Jcomponent present information or interact with the user

• Examples: labels (JLabel), buttons (JButton), text fields (JTextField)

• some JComponents are designed to hold other components, not present or interact, they are called
Containers.
• Examples: JPanel , JScrollPane

• Top Level Windows: are containers that are not contained by any other containers;
they can be dragged and interact with the native windowing system
• Example: JFrame, JDialog (not JComponents at all)

GUI : interpreted by Asaminew G. 01/27/2023


7

GUI component
• AWT component :

GUI : interpreted by Asaminew G. 01/27/2023


8

Cont. …
• Swing components :
• Are part of java foundation classes (JFC) a java library for cross platform GUI
development. The following figure show some basic swing components

• Types of swing components


• Swing container :- panel, pane(split, tapped, scroll, desktop, layered), tool bar…

• Swing controls :- button, text box, label, check box, radio button …

• Swing menus :- menu, menu bar, menu item, popup menu, …

• Swing windows :- frame, dialog, file chooser, color chooser, …

GUI : interpreted by Asaminew G. 01/27/2023


9


• …

GUI : interpreted by Asaminew G. 01/27/2023


10

Swing component
• Swing container
• Top level :- cannot attached to other components i.e. they are stand alone
e.g. - Jframe :- An object of the class Jframe is what you think of as a window. It
automatically has a border and some basic buttons for minimizing the window and similar
actions. As you will see, a JFrame object can have buttons and many other components added
to the window and programmed for action

- Dialog

• Intermediate :- are a container which can be attached on other components

- can be dragged and dropped on other container


e.g. panel, pane(split, tapped, scroll, desktop, layered)

GUI : interpreted by Asaminew G. 01/27/2023


11

Cont. …
• Swing controls
• JButton :- is a component which a user click to trigger specific event

- is a subclass of AbstractButton

- user can be click it to trigger specific action

- has a text or an image/icon as a label

- can access common attributes and behavior from AbstractButton class

AbstractButton :- has JButton and JToggleButton subclasses

JToggleButton :- is called state button

- Has two subclass namely :- JCheckBox and JRadioButton

- selected or deselected

GUI : interpreted by Asaminew G. 01/27/2023


12

Swing button hierarchy


• …

GUI : interpreted by Asaminew G. 01/27/2023


13

Cont. …
• JTextField :- extends class JTextComponent (javax.swing.text)

- provide single line area in which user enter text from keyboard

- display a typed character while user typing in it

- enabled or disabled by user

JPasswordField :- is extends from JTextField

- Hide the actual character

JLabel :- used to display text/image information

GUI : interpreted by Asaminew G. 01/27/2023


14

Cont. …
• Swing menus :-

Menu Item

Menu Bar
Menu

GUI : interpreted by Asaminew G. 01/27/2023


15

Event and event handler


• Event :- drives a program to perform a task

- occur while user interacts with GUI components

- fired by GUI components

- is an object that acts as a signal to another object known as a listener

- The listener object performs some action in response to an event

A given component may have any number of listeners, from zero to several listeners.
Each listener might respond to a different kind of event, or multiple listeners might
respond to the same events.
GUI : interpreted by Asaminew G. 01/27/2023
16


• Event handler : - the code that perform a task in respond to event

- a listener object methods that specify what will happen when


events of various kinds are received by the listener

• Event handling :- is the overall process of responding to an event

GUI : interpreted by Asaminew G. 01/27/2023


17

Type of an event
• Action event :- fired by component such as button, text field, password field …

- handled by action listener which is an object of a class that


implements ActionListener interface. The ActionListener interface has only one
method heading that must be implemented, namely :
public void actionPerformed(ActionEvent e)

- e.g.

GUI : interpreted by Asaminew G. 01/27/2023


18

Cont. …
• Mouse event :- fired when mouse has interaction such as : enter, released, clicked,
exited, and pressed with GUI component

• Mouse motion :- fired when component is dragged or moved within a mouse

• Mouse wheel :- fired when mouse wheel is moved

• Key event :- fired when key is pressed, released and typed

• Window event :- fired when window is opened, closed, closing, activated, …

GUI : interpreted by Asaminew G. 01/27/2023


19

There are also other


types of an event which
are shown on the picture

GUI : interpreted by Asaminew G. 01/27/2023


20

GUI : interpreted by Asaminew G. 01/27/2023

You might also like