0% found this document useful (0 votes)
133 views9 pages

UNIT 5 Oops

The document discusses Java AWT and event handling in Java AWT. It describes AWT components, containers, and windows. It also explains how events are handled through listeners and provides examples of using mouse listeners and action listeners.

Uploaded by

Harendra Tomar
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)
133 views9 pages

UNIT 5 Oops

The document discusses Java AWT and event handling in Java AWT. It describes AWT components, containers, and windows. It also explains how events are handled through listeners and provides examples of using mouse listeners and action listeners.

Uploaded by

Harendra Tomar
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/ 9

➔AWT

• Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based


applications 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 are using the resources of OS.The java.awt
package provides classes for AWT api such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.
Java AWT Hierarchy
The hierarchy of Java AWT classes are given below.
Container
The Container is a component in AWT that can contain another 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.

There are two ways to create a Frame. They are,


• By Instantiating Frame class
• By extending Frame class

Example:
import java.awt.*;
import java.awt.event.*;
class MyLoginWindow extends Frame
{
TextField name,pass;
Button b1,b2;
MyLoginWindow()
{
setLayout(new FlowLayout());
this.setLayout(null);
Label n=new Label("Name:",Label.CENTER);
Label p=new Label("password:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('#');
b1=new Button("submit");
b2=new Button("cancel");
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
this.add(b1);
this.add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(200,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public static void main(String args[])
{
MyLoginWindow ml=new MyLoginWindow();
ml.setVisible(true);
ml.setSize(400,400);
ml.setTitle("my login window");
}
}
Output:

➔Event handling:
Changing the state of an object is known as an event. For example, click on button, dragging mouse
etc. The java.awt.event package provides many event classes and Listener interfaces for event
handling.

Event handling has three main components,


• Events : An event is a change in state of an object.
• Events Source : Event source is an object that generates an event.
• Listeners : A listener is an object that listens to the event. A listener gets notified when an
event occur
How Events are handled?
A source generates an Event and send it to one or more listeners registered with the source. Once
event is received by the listener, they process the event and then return. Events are supported by a
number of Java packages, like java.util, java.awt and java.awt.event.
Important Event Classes and Description Listener Interface
Interface Event Classes
ActionEvent generated when button is ActionListener
pressed, menu-item is selected,
list-item is double clicked
MouseEvent generated when mouse is MouseListener
dragged, moved,clicked,pressed
or released and also when it
enters or exit a component
KeyEvent generated when input is received KeyListener
from keyboard
ItemEvent generated when check-box or list ItemListener
item is clicked
TextEvent generated when value of textarea TextListener
or textfield is changed
MouseWheelEvent generated when mouse wheel is MouseWheelListener
moved
WindowEvent generated when window is WindowListener
activated, deactivated,
deiconified, iconified, opened or
closed
Steps to handle events:
• Implement appropriate interface in the class.
• Register the component with the listener.
How to implement Listener
1. Declare an event handler class and specify that the class either implements an
ActionListener(any listener) interface or extends a class that implements an ActionListener
interface.

For example:

public class MyClass implements ActionListener


{
// Set of Code
}
2. Register an instance of the event handler class as a listener on one or more components.
For example:
someComponent.addActionListener(instanceOfMyClass);
3. Include code that implements the methods in listener interface.
For example:
public void actionPerformed(ActionEvent e)
{
//code that reacts to the action
}

Mouse Listener
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Mouse implements MouseListener
{
TextArea s;
public Mouse()
{
Frame d=new Frame("kkkk");
s=new TextArea("");
d.add(s);
s.addMouseListener(this);
d.setSize(190, 190);
d.show();
}
public void mousePressed(MouseEvent e)
{
System.out.println("MousePressed");
int a=e.getX();
int b=e.getY();
System.out.println("X="+a+"Y="+b);
}
public void mouseReleased(MouseEvent e)
{
System.out.println("MouseReleased");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("MouseEntered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("MouseExited");
}
public void mouseClicked(MouseEvent e)
{
System.out.println("MouseClicked");
}
public static void main(String arg[])
{
Mouse a=new Mouse();
}
}

ACTION LISTENER
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class A extends JFrame implements ActionListener
{
A()
{
JPanel buttonpanel = new JPanel();
JButton b1 = new JButton("Hai");
buttonpanel.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println(“Hai button”);
}
public static void main(String args[])
{
A f = new A();
f.setTitle("ActionListener");
f.setSize(500,500);
f.setVisible(true);
}

Java adapter classes


Java adapter classes provide the default implementation of listener interfaces. If you inherit the
adapter class, you will not be forced to provide the implementation of all the methods of listener
interfaces. So it saves code. The adapter classes are found in java.awt.event, java.awt.dnd and
javax.swing.event packages.

Adapter class Listener interface


WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener

You might also like