Chapter 16 - Event-Driven Programming Flashcards
Clicking a button fires an action event. What do we call the button?
The button is the event source object.
What is the root class of the event classes?
java.util.EventObject
What is the superclass of MouseEvent and KeyEvent? What is this class’ superclass?
InputEvent is the superclass of MouseEvent and KeyEvent. ComponentEvent is the superclass of InputEvent, among others. (The event class hierarchy can be found at page 628).
- Can a button fire a MouseEvent?
- Can a button fire a KeyEvent?
- Can a button fire an ActionEvent?
The answer to all three is yes (I think). Since events are fired from a Component (root class of all GUI classes) source object, a button – JButton, which is a subclass of Component – can fire all the events Component can. In other words: if a component can fire an event, any subclass of the component can fire the same type of event.
In which package are the main event classes located?
They are included in the java.awt.event package, except ListSelectionEvent and ChangeEvent, which are in the javax.swing.event package.
What is an event listener?
An event listener (or simply listener) object is an instance of a listener interface. A listener “listens” for events fired by the source object, and contains methods called event handlers to process the event (make the program do something).
A listener must be registered with an event soure objet, and must also be an instance of an appropriate event-handling interface. E.g. a listener for the event MouseEvent should implement the interface MouseListener and not ActionListener.
How do you register a listener object by a source object?
For ActionEvent, the method is addActionListener. E.g. to register the listener ShrinkListener to the JButton jbtShrink, you type jbtShrink.addActionListener(new ShrinkListener()); In general, the method is named addXListener for XEvent.
An event object is passed to the event handler in the event listener. How do you find out which source object that fired the event?
You can use the method getSource(), which returns the source object for the event.
E.g. e.getSource()
What is an inner class?
An inner class, or nested class, is a class defined within the scope of another class. Inner classes are useful for defining listener classes.
How do you use the methods and data defined in the out class, inside the inner class?
You don’t need to pass anything to the constructor of the inner class. An inner class can use methods and data defined in the outer class by default.
How do you create an instance of the inner class?
You usually do this in the outer class in the normal way, using the "new" keyword. Depending on the visibility modifier of the outer and inner class, you can also create instances of the inner class in other classes. If the inner class is nonstatic, you do this by first creating an instance of the outer class, then using the following syntax: OuterClass.InnerClass innerObject = outerObject.new InnerClass(); if the inner class is static: OuterClass.InnerClass innerObject = new OuterClass.InnerClass();
Why is it helpful to have inner classes?
A simple use of inner classes is to combine dependent classes into a primary class. This reduces the number of source files. a listener class is designed specifically to create a listener object for a GUI component (e.g. a button). The listener class will not be shared by other applications and therefore is appropriate to be defined inside the frame class as an inner class. Another practical use of inner classes is to avoid class-naming conflicts. You can have a public class A defined one place, and also have a class A as an inner class somewhere else.
What is an anonymous inner class?
An anonymous inner class is an inner class without a name. It combines defining an inner class and creating an instance of the class into one step.
What is the syntax for creating an anonymous inner class? (Let’s say you wanted to add an event listener to the JButton jbtShrink)
jbtShrink.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // process event } }
In general, the syntax is: new SuperClassName/InterfaceName() { // implement or override methods in superclass or interface // other methods if necessary }
Typical:
JFrame frame = new JFrame();
frame.pack();
What does the method pack() do?
The pack method can be used instead of the setSize method. setSize is used to explicitly set the frame size, while pack “packs” the frame around the components inside the frame. The frame will be as small as the components inside the frame allow for.
What is the perferred way of defining listener classes?
Using an inner class or an anonymous inner class is perferred.
When defining a listener class, when should you use a regular inner class, and when should you use an anonymous inner class?
In general, use anonymous inner classes if the code in the listener is short and the listener is registered for one event source. Useinner classes if the code in the listener is long or the listener is registered for multiple event sources.
Which methods does the MouseListener interface contain for handling MouseEvent?
MouseListener contains the methods:
mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited.
Which methods does the MouseMotionListener interface contain for handling MouseEvent?
MouseMotionListener contains the methods:
mouseDragged, mouseMoved.
What is a listener interface adapter?
A listener interface adapter is a class that provides the default implementation for all the methods in the listener interface. This means that if you wanted to to use the MouseListener interface, but only needed to implement one of the five abstract methods, you could instead use the MouseAdapter interface adapter. The adapter will have all the abstract methods in MouseListener implemented as empty bodies, meaning that you only need to implement the method you need.