Event handling in AWT and Swing
Download
Report
Transcript Event handling in AWT and Swing
Pravin Yannawar, DOCS, NMU Jalgaon
Objectives of This Session
• Explain the Event handling mechanism &
demonstrate it using example
• List the categories of Events & Listener
interfaces
• Demonstrate the above example using an
independent event handler class
• Identify the need for adapter classes &
demonstrate it using an independent event
handler example
Basic Java : Event handling in AWT and Swing
2
Event Handling
• GUI applications are event-driven applications.
• They generate events when the user of the
program interacts with the GUI.
• The underlying OS is constantly monitoring
these events.
• When a event occurs, the OS reports these
events to the programs that are running.
• The application will handle the event using a
appropriate “Event Handler”.
Basic Java : Event handling in AWT and Swing
3
Delegation Event Model
• Source generates an event & sends it to one or
more listeners.
• Listener simply waits until it receives an event.
• Once received, listener processes the event &
returns.
Advantage : application logic that processes
event is clearly separated from the UI logic that
generates those events.
An UI element is able to delegate the processing
of an event to a separate piece of code ( Event
handler)
Basic Java : Event handling in AWT and Swing
4
Delegation Event Model
• Event: is an object that describes a state
change in a source.
• Event Source: is an object that generates an
event.
A source must register listeners for listeners to
receive notifications about a specific type of
event.
• Event Listener : is an object that is notified
when an event occurs.
It must be registered with a source.
It must implement methods to receive & process
these notifications.
Basic Java : Event handling in AWT and Swing
5
Event Handling
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ActionListener
{
TextField t1;
Button b1;
MyFrame()
{
t1 = new TextField(20);
b1 = new Button(“Click”);
add(t1,”North”);
b1.addActionListener(this);
add(b1,”South”);
}
Basic Java : Event handling in AWT and Swing
6
Event Handling
public void actionPerformed( ActionEvent e )
{
t1.setText(“Click pressed”);
}
public static void main(String args[])
{
Frame f = new MyFrame();
f.setSize(100,100);
f.show();
}
}
Basic Java : Event handling in AWT and Swing
7
Inheritance diagram of the AWT Event Classes
Event
Object
AWT Event
Action
Event
Adjustment
Event
Component
Event
Item
Event
Text
Event
Container
Event
Focus
Event
Input
Event
Paint
Event
Window
Event
Key
Event
Mouse
Event
Basic Java : Event handling in AWT and Swing
8
Event Listener Interfaces
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
java.util.EventListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
TextListener
WindowListener
Basic Java : Event handling in AWT and Swing
9
Event Handling Summary
Interface
Methods
Parameter
Events
Generated by
ActionListner
actionPerformed
ActionEvent
getActionCommand
getmodifiers
Button
List
MenuItem
TextField
Adjustment
Listner
adjustmentValue
Changed
AdjustmentEvent
getAdjustable
getAdjustmentType
getvalue
Scrollbar
ItemListner
itemStateChanged
ItemEvent
getItem
getItemSelectable
getstateChange
Checkbox
CheckboxMenu
Item
Choice
List
TextListner
textValue Changed
TextEvent
TextComponent
Basic Java : Event handling in AWT and Swing
10
Event Handling Summary
Interface
Methods
Parameter
Events
Generated by
Component
Listener
componentMoved
componentHidden
componentResized
ComponentEvent
getComponent
Component
Container
Listener
componentAdded
componentRemoved
ContainerEvent
getChild
getContainer
Container
FocusListener
focusGained
focusLost
focusEvent
IsTemporary
Component
KeyListener
keyPressed
keyRealsed
keyTyped
KeyEvent
getKeyChar
getKeyCode
getkeyModifiersText
isActionKey
Component
Basic Java : Event handling in AWT and Swing
11
Event Handling Summary
Interface
Methods
Parameter
Events
Generated by
Mouse
Listener
mousePressed
mouseRealesed
mouseEntered
mouseExited
mouseClicked
MouseEvent
getClickCount
getX
getY
getPoint
translatePoint
isPopupTrigger
Component
MouseMotion
Listener
mouseDragged
mouseMoved
Window
Listener
windowClosing
windowOpened
windowIconed
windowDeiconed
windowClosed
windowActivated
windowDeactivated
Component
WindowEvent
getWindow
Basic Java : Event handling in AWT and Swing
Window
12
Delegation Event Model with Another Class
import java.awt.*;
class MyFrame extends Frame
{
TextField t1;
Button b1;
MyFrame() {
t1 = new TextField(20);
b1 = new Button(“Click”);
add(t1,”North”);
b1.addActionListener(new ButtonHandler(this));
add(b1,”South”);
}
Basic Java : Event handling in AWT and Swing
13
Delegation Event Model with Another Class
class ButtonHandler implements ActionListener
{
MyFrame f ;
ButtonHandler(MyFrame mf)
{
f=mf;
}
public void actionPerformed( ActionEvent e )
{
// code for processing button press.
}
}
Basic Java : Event handling in AWT and Swing
14
Adapter Classes
• Many of the listener interfaces have more than one
method.
• Thus, if a particular interface is implemented, all the
methods of that interface should also be
implemented.
• To simplify this task, listener interfaces with more
than one method come with adapter classes.
• Adapter classes implement all the methods of an
interface.
•
You can extend the adapter class to specify the
desired reaction to some methods.
Basic Java : Event handling in AWT and Swing
15
Without Adapter Classes
class MyFrame extends Frame implements
WindowListener
{
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
System.exit(0);
}
Basic Java : Event handling in AWT and Swing
16
Without Adapter Class
public static void main(String args[])
{
Frame f = new MyFrame();
f.setSize(100,100);
f.show();
f.addWindowListener(f);
}
}
Basic Java : Event handling in AWT and Swing
17
Using Adapter Class
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
MyFrame()
{
addWindowListener(new
WindowHandler());
}
}
Basic Java : Event handling in AWT and Swing
18
Adapter Classes
class WindowHandler extends WindowAdapter {
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
Basic Java : Event handling in AWT and Swing
19