CS211 Spring 2002 Lecture 12: GUI "dynamics" 2/27/2003 ------------------------------------------------------------------------------- 0) Announcements + reminder about specs... (recent discussion on News) + T1 conflicts? more T1 info (topics, format, etc) + intro to next half of the semester + possible E3 (start A4) Overview + building GUI + GUI "dynamics" - events - listeners ------------------------------------------------------------------------------- 1) GUI Reminders and Process + Overall classification of classes: - Components: pretty much everything you see on the screen - Events: an object that represents an occurrence - Listeners: an object that "listens" for an event + To build a GUI, you will need to learn about - components: what you see on the screen - containers: special kinds of components that contain other comps - layout managers: objects that control placement and sizing of comps - event listeners: objects to respond to events generated by user + Main Execution model: - Write a class with a main() method that creates an object of your new GUI class type. - Write a GUI class that, e.g., extends JFrame. This class's constructor (or other init() method) should: + Create all of the GUI components you will use (objects) + Decide on a Layout + Place all of the objects in the Layout for the JFrame (might use JPanels to nest components) + Define action listeners where appropriate - When the GUI is run, Java will keep track of events that happen and automatically call your listeners when appropriate. ------------------------------------------------------------------------------- 2) Events See http://java.sun.com/docs/books/tutorial/uiswing/overview/event.html http://java.sun.com/docs/books/tutorial/uiswing/events/ + GUI Dynamics: - GUI containers and components are "static" (don't really do anything) - need "action" to interact with user! - use EVENT: a signal to program that something's happened + Java will alert your program when an event happens + something happens if you have written code for that event + examples of events: - button click, typing text, radio box, etc. (ActionEvent) - mouse click (MouseEvent) - moving the mouse cursor (MouseMotionEvent) - closing the window (WindowEvent) - See http://java.sun.com/docs/books/tutorial/uiswing/overview/event.html + General: - SOURCE OBJECT or EVENT SOURCE: usually a component on which the event has happened ex) changing text has JTextComponent source object and generates a TextEvent - each event is an object that extends java.util.EventObject - use getSource() instance method to identify source object in Event - if component can generate an event, its subclasses can, too ------------------------------------------------------------------------------- 3) Packages imports: + you usually need to import java.awt.event + see also need javax.swing.event java.awt.event: http://java.sun.com/j2se/1.4/docs/api/java/awt/event/package-summary.html javax.swing.event: http://java.sun.com/j2se/1.4/docs/api/javax/swing/event/ package-summary.html http://java.sun.com/docs/books/tutorial/uiswing/events/ eventsandcomponents.html java.util: + don't usually need to import this, but it contains ancestors for events and listeners + EventObject (class): http://java.sun.com/j2se/1.4/docs/api/java/util/EventObject.html + EventListener (interface): http://java.sun.com/j2se/1.4/docs/api/java/util/EventListener.html ------------------------------------------------------------------------------- 4) Portion of Event Class Hierarchy EventObject <-- AWTEvent <-- ActionEvent, ComponentEvent,... (AWT) <-- (see javax.swing.event) ComponentEvent <-- ContainerEvent, FocusEvent, InputEvent,.... InputEvent <-- MouseEvent, KeyEvent,... ------------------------------------------------------------------------------- 5) Event Listeners + delegation model: - user action generates action on source object - LISTENER: the object which is "interested" in the event receives event + role of source object: - must register "interested" object as listener - maintains list of listeners - invokes the event-handling method (HANDLER) on listener to respond user =====> Source =====> Event =====> Listener action ^ Object ^ Object ^ Object | | | trigger generate notify event event listener + Huh? - A source object (usually component) will generate an EventObject based on something the user does -> You don't actually write code like "makeMouseEvent()" -> Having a user interact with a component means that the component's event is generated! -> but you do need to specify a handler and listener to make something happen! - The class that's willing to provide code to handle event must implement a listener interface that corresponds to the event -> Event <==> Listener ex) ActionEvent <==> ActionListener -> The "willing" class is a listener! -> General structure: // Declaration for listener (class that handles events), // code that specifies that the class either implements a listener // interface or extends class that implements listener interface: public class MyClass implements SomethingListener { ... } // So, MyClass becomes a listener for SomethingEvents -> Implementing an interface means fleshing out its methods -> Each listener interface has handler (methods that handle what to do for the events) -> example structure for ActionEvents: // Code that implements the methods in the listener interface: public void ActionPerformed(ActionEvent e) { //using Action here ...//code that reacts to the action... } - The listener (object that handles the event) must register its "willingness" with addListener message sent to source object -> When a component generates an event, the event will be sent to its listener (object registered to receive the event) -> If there is no listener, then the event is ignored -> To register willingness of listener, access the source object's addListener method and pass the listener object -> This code goes where the component object "lives". -> general syntax: // Code that registers an instance of the event handler class // as a listener upon one or more components: someComponent.addSomethingListener(instanceOfMyClass); - Notes: + You can have a listener for each type of event. Listeners are associated with the object that the event happens to, and tell Java what to do when the event happens. + Source and listener objects might be identical + Source object might have many listeners - Simple way to help: make source object's container be the listener for source object's events + the container will implement a listener interface + the container will implement a handler + the container will register itself with the component (source object) -------------------------------------------------------------------------------