21- Event handling in java
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
141
On Slideshare
141
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1.  Making Applications Interactive 1
  • 2.  GUIs are event driven  Generate events when user interacts with GUI  e.g., moving mouse, pressing button, typing in text field, etc. 2
  • 3. Event source Event listener Event object Event handling methods 3
  • 4. 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
  • 5.  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
  • 6. 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
  • 7. 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
  • 8. 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 PropertyChangeListene r 8
  • 9.  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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13.  TextListener  Applies to textfields and text areas  textValuedChanged(TextEvent event)  WindowListener  Handles high-level window events  windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated 13
  • 14. Button ActionListener ActionEvent actionPerformed(..) 14
  • 15. o is an event source h 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 ItemListener 15
  • 16. interface ActionListener { public void actionPerformed(ActionEvent e); }  Possible event sources  Button  List  TextField  MenuItem 16
  • 17. 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. 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. 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 class class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent o){ System.exit(0); } } } }