OOPs-Java_ Unit-4
OOPs-Java_ 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.
Prepared by GSK
Java.awt package
GUI(graphical user interface):-
Prepared by GSK
Terminology
• Component : - Component is an object which is
displayed pictorially on the screen.
Ex:- Button,Label,TextField......etc
• Ex: f.setVisible(true);
Prepared by GSK
Characteristics of the Frame Contd…
• Ex: f.setSize(400,500);
Prepared by GSK
Characteristics of the Frame Contd…
• Ex: f.setTitle("MyFrame");
• 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);
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.
Prepared by GSK
What is Event Delegation Model? Cont..
• Event: - Event is nothing but a particular action
generated on the particular 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.
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
Prepared by GSK
How It Works? Contd…
• Note: - To attach a appropriate listener to the Frame we have to
use following method
Prepared by GSK
Event Handling
• Any action that user performs on a GUI component must
be listened and necessary action should to be taken.
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.
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.
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 visibility
f.setVisible(true);
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.
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
}
import java.awt.*;
import java.awt.event.*;
// Create a button
Button button = new Button("Click Me");
Prepared by GSK
Program to work with button component
// Add button to the frame
frame.add(button);
// 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.
• 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.
Prepared by GSK
Java AWT CheckboxGroup
• The object of CheckboxGroup class is used to group
together a set of Checkbox.