Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.

Similar presentations


Presentation on theme: "Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc."— Presentation transcript:

1 Event Handling

2 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc. Class java.awt.AWTEvent

3 3 Some event classes of package java.awt.event 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 4 Event Handling 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 5 Event listener interfaces 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 6 Register event listener for event source component.addXYZListener(an instance of XYZListener) where XYZ is the type of event e.g. aButton.addActionListener(new MyButtonListener()); aFrame.addWindowListener(new MyWindowListener()); aButton.addActionListener(new ActionListener() { // anonymous inner class public void actionPerformed(ActionEvent e) { // code to be executed }});

7 7 Standard AWT Event Listeners ListenerAdapter class (if any)Registration method ActionListeneraddActionListener AdjustmentListeneraddAdjustmentListener ComponentListenerComponentAdapteraddComponentListener ContainerListenerContainerAdapteraddContainerListener FocusListenerFocusAdapteraddFocusListener ItemListeneraddItemListener KeyListenerKeyAdapteraddKeyListener MouseListenerMouseAdapteraddMouseListener MouseMotionListenerMouseAdapteraddMouseMotionListener TextListeneraddTextListener WindowListenerWindowAdapteraddWindowListener

8 8 Some Events and Their Associated Event Listeners Act that Results in the EventListener 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 componentMouseMotionListener Component becomes visibleComponentListener Component gets the keyboard focusFocusListener Table or list selection changesListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener

9 9 Standard AWT Event Listeners 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)

10 10 Standard AWT Event Listeners 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)

11 11 Standard AWT Event Listeners 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

12 12 Standard AWT Event Listeners 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)

13 13 Standard AWT Event Listeners TextListener –Applies to textfields and text areas textValuedChanged(TextEvent event) WindowListener –Handles high-level window events windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated

14 14 Event-dispatching thread Event-handling code executes in an single thread, the event-dispatching thread. This ensures that each event handler finishes execution before the next one executes. event-handling code should execute quickly so that the program’s GUI stays responsive. If an event takes too long to execute, the GUI will freeze--that is, it won’t repaint or respond to mouse clicks.

15 15 Event queue textField1 listenerList... handler This reference is created by the statement textField1.addActionListener( handler ); public void actionPerformed( ActionEvent event ) { // event handled here } JTextField object TextFieldHandler object


Download ppt "Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc."

Similar presentations


Ads by Google