[go: up one dir, main page]

0% found this document useful (0 votes)
20 views64 pages

Unit 2.1_GUI

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views64 pages

Unit 2.1_GUI

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Graphical User Interfaces

Enterprise Application Development

Bhupendra Panchal

4/21/2023 1
Outlines
• Java Beans: Common Elements of Graphical User
Interfaces: Introduction, Main features and Technology of
GUI, Introducing the Java foundation classes, Event
Model, JFC Sample programs, Layout managers, Events,

• Java Beans : Introduction, JavaBeans Component Model,


Bean Development Environments, Using the Sun
BeanBox, Creating a JavaBean Class, Exploring JavaBean
Property Types, Adding Custom Event Types, Creating a
JavaBean Class with Events, Using the BeanInfo Classes.
4/21/2023 2
INTRODUCTION TO GUI
• GUI, which stands for Graphical User Interface, is a user-friendly visual
experience builder for Java applications.

• It comprises graphical units like buttons, labels, windows, icons, menus


and other graphical representations via which users can connect with
an application.

• Swing and JavaFX are two commonly used applications to create GUIs
in Java.

• As of 2014, the most popular GUIs are Microsoft Windows and Mac OS
And, if we talk about mobile devices, the Apple's IOS and Google's
Android Interface are the widely used GUIs.

4/21/2023 3
Basic Components of a GUI
• Pointer: It is a symbol that appears on the display screen. It can be
moved to select commands and objects.

• Pointing device: It allows you move the pointer and select objects on
the screen, e.g. mouse or trackball.

• Icons: It refers to small images on the display screen that represent


commands, files, windows etc. Using pointer and pointing device, you
can execute these commands.

• Desktop: It is the display screen that contains the icons.

4/21/2023 4
Elements of GUI:
A GUI comprises an array of user interface elements. All these elements
are displayed when a user is interacting with an application and they are
as follows:

1. Input commands such as buttons, check boxes, dropdown lists and


text fields.

2. Informational components like icons, labels or notification dialogs.

3. Navigational units like menus, sidebars and breadcrumbs.

4/21/2023 5
GUI KEY Benefits

• It allows you to place more information within a program.

• The graphics allow users to use complex programs with greater

ease.

• It saves time as you do not need to edit configurations manually.

• You can easily memorize the tasks (point-and-click).

• Helps create user-friendly software with a point-and-click

interface.

4/21/2023 6
GUI KEY Benefits

Java provides the following frameworks for GUI programming:


1. Abstract Windowing Toolkit: This is the oldest framework in Java and it was first
introduced in JDK 1.0. Most of the AWT components are now outdated and are
replaced by Java swing components.

2. Swing API: This is a set of graphical libraries developed on top of the AWT framework
and is a part of Java Foundation Classes (JFC). Swing has modular architecture wherein
we can use plug-and-play for the components.

3. JavaFX: The latest framework is available from Java 8 onwards.

4/21/2023 7
ABSTRACT WINDOW TOOLKIT (AWT)

4/21/2023 8
AWT
ABSTRACT WINDOW TOOLKIT (AWT)
Java AWT (Abstract Window Toolkit) is an API to develop GUI or
window-based application in java.

• Java AWT components are platform-dependent i.e. components


are displayed according to the view of operating system.
• AWT is heavyweight i.e. its components uses the resources of
system.
• The Abstract Window Toolkit(AWT) support for applets.
• The AWT contains numerous classes and methods that allow
you to create and manage windows.
• The java.awt package provides classes for AWT api such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice, List
etc.
4/21/2023 9
AWT Classes
The AWT classes are contained in the java.awt package. It is one of Java's largest packages

4/21/2023 10
AWT Classes
The AWT supports the following types of controls:
1. Labels
2. Push buttons
3. Check boxes
4. Choice lists
5. Lists
6. Scroll bars
7. Text editing

4/21/2023 11
Java AWT Class Hierarchy

4/21/2023 12
Java AWT Class Hierarchy
Container: The Container is a component in AWT that can contain other
components like buttons, textfields, labels etc. The classes that extend Container
class are known as container such as Frame, Dialog and Panel.

Window: The window is the container that has no borders and menu bars. You
must use frame, dialog or another window for creating a window.

Panel: The Panel is the container that doesn't contain title bar and menu bars. It
can have other components like button, textfield etc.

Frame: The Frame is the container that contain title bar and can have menu bars.
It can have other components like button, textfield etc.

4/21/2023 13
Java AWT Example
To create simple AWT example, you need a frame. There are two ways to create
a GUI using Frame in AWT.

By extending Frame class (inheritance)


By creating the object of Frame class (association)

4/21/2023 14
Java AWT Example using Inheritance
// importing Java AWT class // frame size 300 width and 300 height
import java.awt.*; setSize(300,300);

// extending Frame class to our class AWTExam // setting the title of Frame
ple1 setTitle("This is our basic AWT example");
public class AWTExample1 extends Frame {

// no layout manager
// initializing using constructor setLayout(null);
AWTExample1() {
// now frame will be visible, by default it is
// creating a button not visible
Button b = new Button("Click Me!!"); setVisible(true);
}
// setting button position on screen public static void main(String args[]) {
b.setBounds(30,100,80,30);
// creating instance of Frame class
// adding button into frame AWTExample1 f = new AWTExample1();
add(b);
4/21/2023 } 15
}
Java AWT Example using Inheritance

4/21/2023 16
Java AWT Example using Association
// importing Java AWT class
import java.awt.*; // adding components into frame
f.add(b);
// class AWTExample2 directly creates instance of Frame class f.add(l);
class AWTExample2 { f.add(t);

// initializing using constructor // frame size 300 width and 300 height
AWTExample2() { f.setSize(400,300);

// creating a Frame // setting the title of frame


Frame f = new Frame(); f.setTitle("Employee info");

// creating a Label // no layout


Label l = new Label("Employee id:"); f.setLayout(null);

// creating a Button // setting visibility of frame


Button b = new Button("Submit"); f.setVisible(true);
}
// creating a TextField public static void main(String args[]) {
TextField t = new TextField();
// setting position of above components in the frame // creating instance of Frame class
l.setBounds(20, 80, 80, 30); AWTExample2 awt_obj = new AWTExample2();
t.setBounds(20, 100, 80, 30);
}
b.setBounds(100, 100, 80, 30);
4/21/2023 17
}
Java AWT Example using Association
Introducing the
Java foundation classes

4/21/2023 19
Introducing the Java foundation classes

4/21/2023 20
Introducing the Java foundation classes
• Java provides a set of features and functionality for developing
graphical user interfaces. This set of features is known as Java
Foundation Classes or JFC.

• In 1997, Sun Microsystems and IBM decided to develop such an API


that application developers could implement on Java class libraries to
make applications accessible.

• As the result, Sun Microsystems wrote the accessibility API and IBM
has tested their API and implemented it on their foundation classes.

• JFC stands for Java Foundation Classes. It is a rich and


comprehensive set of GUI components and services that simplify the
development and deployment of desktop, client-side, Cloud based and
internet applications.

4/21/2023 21
Introducing the Java foundation classes
• Using JFC we can make applications more interactive. JFC contains a
set of graphical components that can be easily plugged into our
application and programmed as per our requirements.

• JFC contains classes from java.awt and javax.swing packages. Apart


from these, it also contains classes related to Java 3D, Java 2D, Java
Accessibility, etc.

4/21/2023 22
Components of JFC

4/21/2023 23
Introducing the Java foundation classes
Advantage of JFC
• Its components are pluggable and require few lines of code.
• It retains Java qualities.
• An application that runs flawlessly on one OS runs flawlessly on
another OS.
• It offers an open architecture.
• Accordingly, the exhibition of a GUI made through JFC is unsurprising.
Therefore, JFC are widely used in client-side applications.

Components of JFC
• The JFC components includes all
the JavaBeans and swing Components that offer re-usability,
interoperability, and portability.

4/21/2023 24
GUI in JAVA: Swing and JavaFX

4/21/2023 25
GUI in JAVA: Swing and JavaFX
• As mentioned, to create a GUI in Java, Swing and JavaFX are
the most commonly used applications.

• Swing was designed with a flexible architecture to make the


elements customizable and easy to plug-and-play that’s why it is
the first choice for java developers while creating GUIs.

• As far as JavaFX is concerned, it consists of a totally different set


of graphic components along with new features and
terminologies.

4/21/2023 26
Swing in JAVA
• Swing in Java is a Graphical User Interface (GUI) toolkit that
includes the GUI components. Swing provides a rich set of
widgets and packages to make sophisticated GUI components
for Java applications.

• Swing is a part of Java Foundation Classes(JFC), which is an


API for Java GUI programming that provide GUI.

• The Java Swing library is built on top of the Java Abstract Widget
Toolkit (AWT), an older, platform dependent GUI toolkit.

• You can use the Java simple GUI programming components like
button, textbox, etc., from the library.

4/21/2023 27
4/21/2023 28
Java Swing class Hierarchy Diagram

All components in Java Swing are JComponent which can be added


to container classes.

4/21/2023 29
Difference between AWT and Swing

4/21/2023 30
4/21/2023 31
4/21/2023 32
What is a Container Class?
Container classes are classes that can have other components on
it. So for creating a Java Swing GUI, we need at least one container
object.

There are 3 types of Java Swing containers.

• Panel: It is a pure container and is not a window in itself. The


sole purpose of a Panel is to organize the components on to a
window.
• Frame: It is a fully functioning window with its title and icons.
• Dialog: It can be thought of like a pop-up window that pops out
when a message has to be displayed. It is not a fully functioning
window like the Frame.

4/21/2023 33
4/21/2023 34
Simple example of Swing by inheritance

import javax.swing.*;
public class Simple2 extends JFrame{
//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");
//create button
b.setBounds(130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}
4/21/2023 35
Example of Swing by Association inside constructor

import javax.swing.*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButt


on
b.setBounds(130,100,100, 40);
f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
public static void main(String[] args) {
new Simple();
} 4/21/2023 36
}
Java JLabel Example
import javax.swing.*;
class LabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
4/21/2023 37
Java JPasswordField Example

import javax.swing.*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}

4/21/2023 38
Registration Form Example

4/21/2023 39
Registration Form Example
package JDBCDemo;
import java.awt.Container;
import java.awt.Font;
import javax.swing.*;
public class SwingDemo {
public static void main(String args[]) {

JFrame frame=new JFrame();


frame.setTitle("Student Register Form");
frame.setVisible(true);
frame.setBounds(500, 100, 500, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);

JLabel message = new JLabel("Register a new Student");


message.setFont(new Font("Courier", Font.BOLD, 20));
frame.add(message);

JLabel nameLabel= new JLabel("Name");


JTextField nameField = new JTextField();
frame.add(nameLabel);
4/21/2023 40
frame.add(nameField);
JLabel genderLabel = new JLabel("Gender");
JRadioButton genderMale = new JRadioButton("Male", true);
JRadioButton genderFemale = new JRadioButton("Female");
frame.add(genderLabel);
frame.add(genderMale);
frame.add(genderFemale);
ButtonGroup genderGroup= new ButtonGroup();
genderGroup.add(genderMale);
genderGroup.add(genderFemale);

JLabel mobileNoLabel = new JLabel("Mobile No");


JTextField mobileNoField = new JTextField();
frame.add(mobileNoField);
frame.add(mobileNoLabel);

JLabel passwordLabel=new JLabel("Password");


JLabel rePasswordLabel=new JLabel("Re Password");
JPasswordField passwordField=new JPasswordField();
JPasswordField rePasswordField=new JPasswordField();
frame.add(passwordField);
frame.add(passwordLabel);
frame.add(rePasswordField);
4/21/2023 41
frame.add(rePasswordLabel);
JLabel programLabel=new JLabel("Courses");
JComboBox<String> programList=new JComboBox<String>();
programList.addItem("ME/M Tect");
programList.addItem("BE/B Tect");
programList.addItem("Ph.D.");
frame.add(programLabel);
frame.add(programList);

JLabel branchLabel=new JLabel("Branch");


JComboBox<String> branchList = new JComboBox<String>();
branchList.addItem("Computer Science and Engineering");
branchList.addItem("Electronics and Telecommunications");
branchList.addItem("Information Technology");
branchList.addItem("Electrical Engineering");
branchList.addItem("Electrical and Electronics Engineering");
branchList.addItem("Civil Engineering");
frame.add(branchLabel);
frame.add(branchList);

JButton registerButton= new JButton("Register");


frame.add(registerButton);
4/21/2023 42
message.setBounds(50, 10, 600, 30);
nameLabel.setBounds(50, 60, 100, 30);
nameField.setBounds(130, 60, 200, 30);
genderLabel.setBounds(50, 160, 100, 30);
genderMale.setBounds(130, 160, 100, 30);
genderFemale.setBounds(240, 160, 100, 30);
mobileNoLabel.setBounds(50, 260, 100, 30);
mobileNoField.setBounds(130, 260, 200, 30);
passwordLabel.setBounds(50, 310, 100, 30);
passwordField.setBounds(130, 310, 200, 30);
rePasswordLabel.setBounds(50, 360, 100, 30);
rePasswordField.setBounds(130, 360, 200, 30);
programLabel.setBounds(50, 410, 100, 30);
programList.setBounds(130, 410, 200, 30);
branchLabel.setBounds(50, 460, 100, 30);
branchList.setBounds(130, 460, 200, 30);
registerButton.setBounds(130, 550, 200, 30);
}
}

4/21/2023 43
Creating a chat frame

4/21/2023 44
Creating a chat frame: Code
The following is the code for creating a chat frame:

package JDBCDemo;
import java.awt.BorderLayout;
import javax.swing.*;
public class MyFirstGUI {
public static void main(String args[]){
try {
//Create the Frame
JFrame jframe = new JFrame("Chat Screen");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(400, 400);
// create two menubar button FILE and HELP
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("FILE");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);

4/21/2023 45
Creating a chat frame: Code
// create two more option in FILE button
JMenuItem fileMenu1 = new JMenuItem("new file");

JMenuItem fileMenu2 = new JMenuItem("Save as");

fileMenu.add(fileMenu1);
fileMenu.add(fileMenu2);
// Text Area at the Center
JTextArea textArea = new JTextArea();
//Create the panel at bottom and add label, textArea and buttons
JPanel panel = new JPanel(); // this panel is not visible in
output
JLabel label = new JLabel("Please Enter Text");

JTextField textField = new JTextField(15);


// accepts upto 15 characters
JButton btn_send = new JButton("Send");

JButton btn_reset = new JButton("Reset");


4/21/2023 46
Creating a chat frame: Code
panel.add(label); // Components Added using Flow Layout
panel.add(textField);
panel.add(btn_send);
panel.add(btn_reset);
//Adding Components to the frame.
jframe.getContentPane().add(BorderLayout.SOUTH,
panel);
jframe.getContentPane().add(BorderLayout.NORTH,
menuBar);
jframe.getContentPane().add(BorderLayout.CENTER,
textArea);
jframe.setVisible(true);

}
catch(Exception e)
{
}
}
}
4/21/2023 47
Java LayoutManagers

4/21/2023 48
Java LayoutManagers
The LayoutManagers are used to arrange components in a particular
manner. The Java LayoutManagers facilitates us to control the
positioning and size of the components in GUI forms.

There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.

4/21/2023 49
Java BorderLayout

• The BorderLayout is used to arrange the components in five regions:


north, south, east, west, and center. Each region (area) may contain
one component only. It is the default layout of a frame or window.

The BorderLayout provides five constants for each region:


1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:


• BorderLayout(): creates a border layout but with no gaps between the
components.
• BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
4/21/2023 50
Example of BorderLayout class: Using BorderLayout() constructor
import java.awt.*;
import javax.swing.*;
public class borderLayout
{
JFrame f = new JFrame();
borderLayout()
{
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new borderLayout();
}
} 4/21/2023 51
Example of BorderLayout class: Using BorderLayout() constructor

4/21/2023 52
Java GridLayout

• The Java GridLayout class is used to arrange the components in a


rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class

1. GridLayout(): creates a grid layout with one column per component in


a row.
2. GridLayout(int rows, int columns): creates a grid layout with the
given rows and columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid
layout with the given rows and columns along with given horizontal and
vertical gaps.

4/21/2023 53
Example of GridLayout class:
// import statements
import java.awt.*;
// adding buttons to the frame
import javax.swing.*;
// since, we are using the parameterless constructor, therfore;

public class GridLayoutExample


// the number of columns is equal to the number of buttons we
{
JFrame frameObj; // are adding to the frame. The row count remains one.
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
// constructor
GridLayoutExample() frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
{
frameObj = new JFrame(); frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);

// creating 9 buttons
JButton btn1 = new JButton("1"); // setting the grid layout using the parameterless constructor
JButton btn2 = new JButton("2");
frameObj.setLayout(new GridLayout());
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
frameObj.setSize(300, 300);
JButton btn6 = new JButton("6");
frameObj.setVisible(true);
JButton btn7 = new JButton("7");
}
JButton btn8 = new JButton("8");
JButton4/21/2023
btn9 = new JButton("9"); 54
// main method
Example of GridLayout class:

4/21/2023 55
Java FlowLayout

• The Java FlowLayout class is used to arrange the components in a line, one
after another (in a flow). It is the default layout of the applet or panel.

Fields of FlowLayout class


1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING

Constructors of FlowLayout class


1. FlowLayout(): creates a flow layout with centered alignment and a default 5
unit horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a
default 5 unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
4/21/2023 56
Example of FlowLayout class:
// import statements
import java.awt.*;
// adding buttons to the frame
import javax.swing.*;
// since, we are using the parameterless constructor, therfore;

public class FlowLayoutExample


// the number of columns is equal to the number of buttons we
{
JFrame frameObj; // are adding to the frame. The row count remains one.
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
// constructor
FlowLayoutExample() frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
{
frameObj = new JFrame(); frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);

// creating 9 buttons
JButton btn1 = new JButton("1"); // setting the Flow layout using the parameterless constructor
JButton btn2 = new JButton("2");
frameObj.setLayout(new FlowLayout());
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
frameObj.setSize(300, 300);
JButton btn6 = new JButton("6");
frameObj.setVisible(true);
JButton btn7 = new JButton("7");
}
JButton btn8 = new JButton("8");
JButton4/21/2023
btn9 = new JButton("9"); 57
// main method
Java FlowLayout

frameObj.setLayout(new FlowLayout());

4/21/2023 58
Java BoxLayout

• The Java BoxLayout class is used to arrange the components either vertically
or horizontally. For this purpose, the BoxLayout class provides four constants.
They are as follows:
Fields of BoxLayout Class
1. public static final int X_AXIS: Alignment of the components are horizontal
from left to right.
2. public static final int Y_AXIS: Alignment of the components are vertical from
top to bottom.
3. public static final int LINE_AXIS: Alignment of the components is similar to
the way words are aligned in a line, which is based on the
ComponentOrientation property of the container.
4. public static final int PAGE_AXIS: Alignment of the components is similar to
the way text lines are put on a page, which is based on the ComponentOrientation
property of the container.
Constructor of BoxLayout class
1. BoxLayout(Container c, int axis): creates a box layout that arranges the
components with the given axis.

4/21/2023 59
Java BoxLayout
import java.awt.*;
import javax.swing.*;

public class BoxLayoutExample1 extends Frame {


Button buttons[];

public BoxLayoutExample1 () {
buttons = new Button [5];

for (int i = 0;i<5;i++) {


buttons[i] = new Button ("Button " + (i + 1));
// adding the buttons so that it can be displayed
add (buttons[i]);
}
// the buttons will be placed horizontally
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
public static void main(String args[]){
BoxLayoutExample1 b=new BoxLayoutExample1();
}
} 4/21/2023 60
Event Model in Java

• The Event model is defined to handle events in GUI programming


languages.
• The GUI programming is inherently event-driven; whenever a user initiates
an activity such as a mouse activity, clicks, scrolling, etc., each is known as
an event that is mapped to a code to respond to functionality to the user.
This is known as event handling.

In this model, a
source generates an
event and forwards
it to one or more
listeners. The
listener waits until it
receives an event.
Once it receives the
event, it is
processed by the
listener and returns
it.
4/21/2023 61
Event Model in Java

Basically, an Event Model is based on the following three components:


• Events
• Events Sources
• Events Listeners

• The Events are the objects that define state change in a source. An event
can be generated as a reaction of a user while interacting with GUI
elements.

• A source is an object that causes and generates an event. A source must


register a listener to receive notifications for a specific event.
Add Listener: public void addTypeListener (TypeListener e1)
Remove Listener:
public void removeTypeListener(TypeListener e2?)

• An event listener is an object that is invoked when an event triggers. For


example, the MouseMotionListener interface provides two methods when
the mouse is dragged and moved.
4/21/2023 62
Java Event classes and Listener interfaces

Event Classes Listener Interfaces


ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener

4/21/2023 63
Java Event classes and Listener interfaces
package JDBCDemo;
import java.awt.*;
import java.awt.event.*;
class JavaModel extends Frame implements ActionListener {
JavaModel()
{
Frame f=new Frame();
TextField textField = new TextField();
f.add(textField); textField.setBounds(60, 50, 180, 25);
Button button = new Button("click Here");
button.setBounds(100, 120, 80, 30);
button.addActionListener(this);
add(textField);
add(button); setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
textField.setText("GFG!");
}
public static void main(String[] args)
{
new JavaModel();
}
} 4/21/2023 64

You might also like