Upload
raja-kumar
View
122
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Making Applications Interactive
1
GUIs are event driven Generate events when user interacts with GUI
e.g., moving mouse, pressing button, typing in text field, etc.
2
Event source Event listener
Event object
Event handling methods
3
Object
EventObject
AWTEvent
ActionEvent
AdjustmentEvent
ItemEvent
TextEvent
ContainerEvent
FocusEvent
PaintEvent
WindowEvent
InputEvent
MouseWheelEvent
ComponentEvent
KeyEvent MouseEvent
Object
EventObject
AWTEvent
ComponentEvent
TextEvent
ItemEvent
AdjustmentEvent
ActionEvent
WindowEvent
InputEvent
MouseEventKeyEvent
MouseWheelEvent
FocusEvent
PaintEvent
ContainerEvent
4
Event-handling model Three parts
Event source GUI component with which user interacts
Event object Encapsulates information about event that occurred
Event listener Receives event object when notified, then responds
Programmer must perform two tasks Register event listener for event source Implement event-handling method (event handler)
5
interface EventListener
interface ActionListener
interface AdjustmentListener
interface ComponentListener
interface ContainerListener
interface FocusListener
interface ItemListener
interface KeyListener
interface MouseListener
interface MouseMotionListener
interface TextListener
interface WindowListener
«interface»EventListener
«interface»ActionListener
«interface»AdjustmentListener
«interface»ComponentListener
«interface»ContainerListener
«interface»FocusListener
«interface»ItemListener
«interface»KeyListener
«interface»MouseListener
«interface»MouseMotionListener
«interface»TextListener
«interface»WindowListener
6
Listener Adapter class (if any) Registration method
ActionListener addActionListener
AdjustmentListener addAdjustmentListener
ComponentListener ComponentAdapter addComponentListener
ContainerListener ContainerAdapter addContainerListener
FocusListener FocusAdapter addFocusListener
ItemListener addItemListener
KeyListener KeyAdapter addKeyListener
MouseListener MouseAdapter addMouseListener
MouseMotionListener MouseAdapter addMouseMotionListener
TextListener addTextListener
WindowListener WindowAdapter addWindowListener
7
Act that Results in the Event Listener Type
User clicks a button, presses Enter while typing in a text field, or chooses a menu item
ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is over a component
MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as the text on a label
PropertyChangeListener
8
ActionListener Handles buttons and a few other actions
actionPerformed(ActionEvent event)
AdjustmentListener Applies to scrolling
adjustmentValueChanged(AdjustmentEvent event)
ComponentListener Handles moving/resizing/hiding GUI objects
componetResized(ComponentEvent event) componentMoved(ComponentEvent event) componentShown(ComponentEvent event) componentHidden(ComponentEvent event)
9
ContainerListener Triggered when window adds/removes GUI controls componentAdded(ContainerEvent event) componentRemoved(ContainerEvent event)
FocusListener Detects when controls get/lose keyboard focus focusGained(FocusEvent event) focusLost(FocusEvent event)
10
ItemListener Handles selections in lists, checkboxes, etc.
itemStateChanged(ItemEvent event)
KeyListener Detects keyboard events
keyPressed(KeyEvent event) – any key pressed down
keyReleased(KeyEvent event) – any key released keyTyped(KeyEvent event) – key for printable char
released
11
MouseListener Applies to basic mouse events
mouseEntered(MouseEvent event) mouseExited(MouseEvent event) mousePressed(MouseEvent event) mouseReleased(MouseEvent event) mouseClicked(MouseEvent event) – release
without drag Applies on release if no movement since pressed
MouseMotionListener Handles mouse movement
mouseMoved(MouseEvent event) mouseDragged(MouseEvent event)
12
TextListener Applies to textfields and text areas
textValuedChanged(TextEvent event)
WindowListener Handles high-level window events
windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated
13
Button ActionListener
ActionEvent
actionPerformed(..)
14
o is an event sourceh is an event listener of type XXX
o.addXXX(h)
where XXX is one of the following:
ActionListener
MouseListener
MouseMotionListener
KeyListener
WindowListener
ComponentListener
FocusListener
TextListener
AdjustmentListener
ItemListener15
interface ActionListener {
public void actionPerformed(ActionEvent e);
} Possible event sources
Button List TextField MenuItem
16
import javax.swing.*;import java.awt.event.*;public class TestButtonAction { public static void main(String[] args){ JFrame f = new JFrame("TestButton"); f.setSize(200,200); JButton hw = new JButton("Hello World!"); f.add(hw); hw.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
System.exit(0); } }); f.setVisible(true); }}
17
18
class MyFrame extends JFrame implements ActionListener { Button hw; public MyFrame(){
super("Test Button");setSize(200,200);hw = new Button("Hello World!");add(hw);hw.addActionListener(this);setVisible(true);
} public void actionPerformed(ActionEvent o){
System.exit(0); }}
19
class MyFrame extends JFrame { Button hw; public MyFrame(){
super("Test Button");setSize(200,200);hw = new Button("Hello World!");add(hw);hw.addActionListener(new MyActionListener());setVisible(true);
//Inner classclass MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent o){System.exit(0);
}}
}}