0% found this document useful (0 votes)
3 views

OOPs-Java_ Unit-4

The document provides an overview of the Java AWT package, which is essential for developing graphical user interface (GUI) applications. It covers key concepts such as components, containers, events, and the event delegation model, along with practical examples of creating frames, buttons, and handling events. Additionally, it discusses layout managers that organize components within a container, including BorderLayout, FlowLayout, GridLayout, and CardLayout.

Uploaded by

amreen2825
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)
3 views

OOPs-Java_ Unit-4

The document provides an overview of the Java AWT package, which is essential for developing graphical user interface (GUI) applications. It covers key concepts such as components, containers, events, and the event delegation model, along with practical examples of creating frames, buttons, and handling events. Additionally, it discusses layout managers that organize components within a container, including BorderLayout, FlowLayout, GridLayout, and CardLayout.

Uploaded by

amreen2825
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/ 43

Unit-4

Event Handling

Prepared by GSK
Java.awt package
• Java.awt is a package it will provide very good
environment to develop graphical user interface
applications.

• The java.awt provides classes for creating a variety of


components such as windows, buttons, text fields, and
event handling mechanisms.

• AWT means Abstract Window Toolkit

Prepared by GSK
Java.awt package
 GUI(graphical user interface):-

• 1. It is a mediator between end


user and the program.

• 2. AWT is a package it will


provide very good predefined
support to design GUI
applications.

Prepared by GSK
Terminology
• Component : - Component is an object which is
displayed pictorially on the screen.

Button b=new Button(); Ex: submit

Ex:- Button,Label,TextField......etc

• Container:- Container is a GUI component, it is


able to accommodate all other GUI components.
Ex:- Frame, window, Applet.
Terminology contd…

• Event:- The event is nothing but a action


generated on the component or the change is
made on the state of the object.

Ex:- Button clicked, Checkbox checked, Item


selected in the list, Scrollbar scrolled
horizontal/vertically.
AWT Class hierarchy
Frame
• 1) Frame is a class which is present in java.awt
package.

• 2) Frame is a Basic component in AWT, because


all the components displayed in a Frame.

• 3) We are displaying pictures on the Frame.

• 4) It is possible to display some text on the


Frame. Based on the above reasons the frame
will become basic component in AWT.
Prepared by GSK
Characteristics of the Frame
• create a Frame class object. Frame f=new Frame();

• 1) When we create a Frame class object the Frame will


be created automatically with the invisible mode. To
provide visible mode use SetVisible method.

• public void setVisible(boolean b)

• where b==true means visible mode. where b==false


means invisible mode.

• Ex: f.setVisible(true);

Prepared by GSK
Characteristics of the Frame Contd…

• 2) When we created a Frame the initial size of


the Frame is : 0 pixel height, 0 pixel width So it is
not visible to use.

• To provide particular size to the Frame we have


to use setSize( ) method.

• public void setSize(int width, int height)

• Ex: f.setSize(400,500);

Prepared by GSK
Characteristics of the Frame Contd…

• 3) To provide title to the Frame explicitly we have to use the


setTitle( ) method. public void setTitle(String Title)

• Ex: f.setTitle("MyFrame");

• 4) When we create a Frame, the default background color of


the Frame is white. If you want to provide particular color to
the Frame we have to use the setBackground method.

• public void setBackground(color_Name)

• Ex: f.setBackground(Color.red);

Prepared by GSK
Example Program to create Frame

import java.awt.*;
class FrameCreation
{
public static void main(String[] args)
{
//frame creation
Frame f=new Frame(); Output

//set visibility
f.setVisible(true);

//set the size of the frame


f.setSize(400,400);

//set the background


f.setBackground(Color.red);

//set the title of the frame


f.setTitle("myframe");
}
}
Prepared by GSK
Why Event Delegation Model?
• The frame can be minimized, maximized but cannot
be closed even if we click on the close button of the
frame it will not perform any closing action now the
question is how to close the frame.

• Closing a frame means attaching action to the


component.

• To attach actions to the components we need event


delegation model.

Prepared by GSK
What is Event Delegation Model?
• 1. When we create a component the components visible on
the screen but it is not possible to perform any action.

• 2. The Frame is a static component so it is not possible to


perform actions on the Frame.

• 3. To make static component into dynamic component we


have to add some actions to the Frame.

• 4. To attach actions to the Frame component we need


event delegation model.

Prepared by GSK
What is Event Delegation Model? Cont..
• Event: - Event is nothing but a particular action
generated on the particular component.

• 1. When an event generates on the component


the component is unable to respond because
component can't listen the event.

• 2. To make the component listen the event we


have to add listeners to the component.

Prepared by GSK
What is Event Delegation Model? Cont..
• 3. Wherever we are adding listeners to the
component the component is able to respond
based on the generated event.

• 4. A listener is a interface which contain abstract


methods and it is present in java.awt.event
package

• 5. The listeners are different from component to


component.

Prepared by GSK
What is Event Delegation Model? Cont..
• A component delegate (Sends) event to the listener and
listener is designates (handover) the event to appropriate
method by executing that method only the event is
handled. This is called Event Delegation Model.

Prepared by GSK
Key Components of the Event Delegation Model

• Event Source: The object that generates an event.


Examples include buttons, text fields, and other GUI
components.

• Event Listener: An interface that receives and


processes events. Listeners must implement specific
methods to handle the events.

• Event Object: An object that encapsulates


information about the event, such as the source of the
event and additional data.
Prepared by GSK
How It Works?
• Event Registration: The event listener is registered with
the event source. This is done using the addListener
method. For example, a button can register an
ActionListener to handle button clicks.

• Event Generation: When an event occurs (e.g., a button


is clicked), the event source creates an event object and
passes it to the registered listener.

• Event Handling: The event listener processes the event


using the methods defined in the listener interface.

Prepared by GSK
How It Works? Contd…
• Note: - To attach a appropriate listener to the Frame we have to
use following method

• Public void addxxxListener(xxxListener e)

• Where xxx may be ActionListener, windowListener etc

• The Appropriate Listener for the Frame is “WindowListener”

• Note: This addxxxListener method has a parameter that is


expecting object of WindowListner. Since it is not possible to
create an object for interface, we should create object for the
implementation class.

Prepared by GSK
Event Handling
• Any action that user performs on a GUI component must
be listened and necessary action should to be taken.

• For example, if a user clicks on a Exit button, then we


need to write code to exit the program. So for this, we
need to know that the user has clicked the button. This
process of knowing is called as listening and the action
done by the user is called an event. Writing the
corresponding code for a user action is called as Event
handling.

Prepared by GSK
Event Handling
• An event listener in Java is an interface that
contains methods called handlers in which
corresponding action code is to be written.

• An event class contains the information about


an event.

• Event source is the GUI component or model on


which an event is generated or in other words an
action is done.

Prepared by GSK
Dispatching of events
• For every action user performs, a corresponding
event object (component) is generated. This
generated event object should be sent to the
corresponding listener so that we can handle that
event and write the code accordingly.

• The process of sending of event object to its


corresponding listener is called as event
dispatching.

Prepared by GSK
import java.awt.*;
Example Program to create Frame
class FrameClosing2 extends Frame then close it by clicking the close
{ button
public static void main(String[] args)
{
//frame creation
FrameClosing2 f=new FrameClosing2();

//set the title


f.setTitle(“myframe");

//set visibility
f.setVisible(true);

//set the size of the frame


f.setSize(400,400);

//set the background


f.setBackground(Color.red);

//Close the frame


f.addWindowListener(new MyClass());
}}
Prepared by GSK
Class MyClass implements WindowListener
{
public void windowActivated(WindowEvent e){ }
public void windowClosed(WindowEvent e){ }
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated (WindowEvent e){ }
public void windowDeiconified (WindowEvent e){ }
public void windowIconified (WindowEvent e){ }
public void windowOpened (WindowEvent e){ }
} Output

Prepared by GSK
Adapter Class in java
• An adapter class is an abstract class
implementing a listener interface. This is essential
when we don't want to write all the handlers.

• For example, MouseListener interface contains a


lot of methods such
as mousePressed(), mouseReleased().. and we
want to write only one of them, we use adapter
class.

Prepared by GSK
import java.awt.*;
class FrameClosing
import java.awt.event.*;
{
public static void main(String[] args)
class MyFrame extends Frame
{
{
MyFrame f=new MyFrame();
}
MyFrame()
} Example Program to create Frame
{
this.setVisible(true); then close it by clicking the close
this.setSize(500,500); button using adapter class
this.setBackground(Color.red);
this.setTitle(" myframe ");
this.addWindowListener(new MyListener());
} Output
}

class MyListener extends WindowAdapter


{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
Prepared by GSK
Java AWT Button
• A button is basically a control component with a label that
generates an event when pushed. The Button class is used
to create a labeled button that has platform independent
implementation. The application result in some action when
the button is pushed.

• To perform an action on a button being pressed and


released, the ActionListener interface needs to be
implemented. The registered new listener can receive
events from the button by
calling addActionListener method of the button.
Prepared by GSK
Program to work with button component

import java.awt.*;
import java.awt.event.*;

public class ButtonExample {


public static void main(String[] args) {
Frame frame = new Frame("Button Example");

// Create a button
Button button = new Button("Click Me");

// Set button's position and size


button.setBounds(50, 100, 80, 30);

// Add action listener to the button


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});

Prepared by GSK
Program to work with button component
// Add button to the frame
frame.add(button);

// Set frame properties


frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);

// Close the frame when the user clicks the close button
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
}

Prepared by GSK
Java AWT Label
• The object of the Label class is a component for placing
text in a container.

• It is used to display a single line of read only text.

• The text can be changed by a programmer but a user


cannot edit it directly.

• It is called a passive control as it does not create any


event when it is accessed.

• To create a label, we need to create the object


of Label class.
Java AWT TextField
• The object of a TextField class is a text component that
allows a user to enter a single line text and edit it.

• When we enter a key in the text field (like key pressed,


key released or key typed), the event is sent
to TextField.

• Then the KeyEvent is passed to the


registered KeyListener.

• It can also be done using ActionEvent;

• The event is handled by the ActionListener interface.


Prepared by GSK
Java AWT TextArea
• The object of a TextArea class is a multiline region that
displays text. It allows the editing of multiple line text. It
inherits TextComponent class it means that TextField is a
subclass of TextComponent.

• The text area allows us to type as much text as we want.

• When the text in the text area becomes larger than the
viewable area, the scroll bar appears automatically
which helps us to scroll the text up and down, or right and
left.

Prepared by GSK
Java AWT Checkbox
• The Checkbox class is used to create a
checkbox.

• It is used to turn an option on (true) or off (false).


Clicking on a Checkbox changes its state from
"on" to "off" or from "off" to "on".

Prepared by GSK
Java AWT CheckboxGroup
• The object of CheckboxGroup class is used to group
together a set of Checkbox.

• At a time only one check box button is allowed to be in "on"


state and remaining check box button in "off" state. It inherits
the object class

• Note: CheckboxGroup enables you to create radio buttons in


AWT. There is no special control for creating radio buttons in
AWT.
public class CheckboxGroup extends Object implements
Serializable .
Prepared by GSK
Layout Managers
• The class that is responsible for determining the size
and position of each component within a container
based on a set of rules or algorithms is called a layout
manager in java

• The layout manager in java takes into account the size


of the container and the preferred size of the
components, as well as any constraints that have been
set for the layout.
Types of the Layout Manager in
Java
1. Border Layout:

• The layout manager BorderLayout divides the


container’s five regions into the north, south, east,
west, and center.

• When a component is added to the container, it is put


in one of these regions and fills the entire region.

• The old component is replaced by the new one if a


component is added to a region that already has one.
Note that the CENTER component takes up all the
remaining space in the container after the other
components have been placed.
Types of the Layout Manager in
Java Contd..
2. Flow Layout:

• A layout manager called FlowLayout arranges


components in a row, adding additional rows as
needed when the width of the container is exceeded.

• From left to right, the components are added, with the


next component being added directly to the right of the
one before it.
Types of the Layout Manager in
Java Contd..
Types of the Layout Manager in
Java Contd..
3. Grid Layout:

• The layout manager GridLayout arranges


elements in a grid of rows and columns.

• The layout manager is created with a specified


number of rows and columns, and components
are added one at a time, filling each grid cell from
left to right and from top to bottom.
Types of the Layout Manager in
Java Contd..
Types of the Layout Manager in
Java Contd..
4. Card Layout:

• The Java CardLayout class manages the components


in such a manner that only one component is visible
at a time. It treats each component as a card that is
why it is known as CardLayout.

• CardLayout is a layout manager that enables switching


between multiple components while keeping them in
the same container by using functions like next() and
previous ( ).
Types of the Layout Manager in
Java Contd..

You might also like