1 / 17

CS202 Java Object Oriented Programming GUI Programming – Event Handling

CS202 Java Object Oriented Programming GUI Programming – Event Handling. Chengyu Sun California State University, Los Angeles. Events. Mouse clicks Mouse movement Keyboard input Window state changes (minimized, maximized, closed) Table or list selection changes …. GUI Toolkit.

mgailey
Download Presentation

CS202 Java Object Oriented Programming GUI Programming – Event Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS202 Java Object Oriented ProgrammingGUI Programming – Event Handling Chengyu Sun California State University, Los Angeles

  2. Events • Mouse clicks • Mouse movement • Keyboard input • Window state changes (minimized, maximized, closed) • Table or list selection changes • …

  3. GUI Toolkit User Input GUI Toolkit Application Code Do something with keyboard input keyboard GUI Component ??? mouse Do something with mouse input other Do something with other input

  4. Callback Functions and Events User Input GUI Toolkit Application Code Keyboard event Do something with keyboard input FunctionA keyboard GUI Component Mouse event mouse Do something with mouse input FunctionB other Other event Do something with other input FunctionC

  5. Callback Functions and Event • Callback functions • Function names are pre-defined – so the GUI toolkit knows which function to invoke when it receive some user input • No function implementation – exactly what to do with the input is left to the application • Events • Parameters to the callback functions • Information about the user input • Which key is pressed, which button is clicked, position of the cursor …

  6. Java Event Handling • Application • Event handling code must implement some event listener interface • Event handler must be registered to some GUI component(s) • component.addEventListener( EventHandler ) • System • When certain event happens, system creates an event object • Invokes event handling code with the event object as parameter

  7. Swing Events and Listeners • http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponents.html • Three important types of events • Action events • Mouse events • Mouse motion events

  8. Action Events • Clicks a button, presses Return when typing in a text field, or chooses a menu item • ActionEvent class • getSource() • getActionCommand()

  9. Handle Action Events • Register to listen to Action Events • component.addActionListener( EventHandler ) • EventHandler must be an object of a class which implements the ActionListener interface public interface ActionListener { public void actionPerformed( ActionEvent e ); }

  10. Mouse Events • Presses a mouse button while the cursor is over a component • MouseEvent class • getButton() • getClickCount() • getX() • getY()

  11. Handle Mouse Events • Register to listen to Mouse Events • component.addMouseListener( EventHandler ) • EventHandler must be an object of a class which implements the MouseListener interface public interface MouseListener { public void mouseClicked( MouseEvent e ); public void mouseEntered( MouseEvent e ); public void mouseExited( MouseEvent e ); public void mousePressed( MouseEvent e ); public void mouseReleased( MouseEvent e ); }

  12. Mouse Motion Events • Moves the mouse over a component • Mouse motion events are also represented by the MouseEvent class

  13. Handle Mouse Motion Events • Register to listen to Mouse Motion Events • component.addMouseMotionListener( EventHandler ) • EventHandler must be an instance of a class which implements the MouseMotionListener interface public interface MouseMotionListener { public void mouseMoved( MouseEvent e ); public void mouseDragged( MouseEvent e ); }

  14. Mouse Event Example • DrawLines.java

  15. Better Event Handling Code – Inner Class • Do we have to use the main class as the event handler? public class EventDemo extends JFrame { SomeEventHandler h = new SomeEventHandler(); Jbutton b = new Jbutton( “Button” ); b.addActionListener( h ); … … class SomeEventHandler implements ActionListener { … … } }

  16. Better Event Handling Code – Adapter Class • Do we have to leave all those empty methods around? public class EventDemo extends JFrame { SomeEventHandler h = new SomeEventHandler(); JButton b = new JButton( “Button” ); b.addMouseListener( h ); … … class SomeEventHandler extends MouseAdapter { … … } }

  17. Better(?) Event Handling Code – Anonymous Inner Class • Do we have to use the same class to handle the events for all components? public class EventDemo extends JFrame { JButton b = new JButton( “Button” ); b.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { … } }); … … }

More Related