Chapter 16 - Event-Driven Programming Flashcards

1
Q

Clicking a button fires an action event. What do we call the button?

A

The button is the event source object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the root class of the event classes?

A

java.util.EventObject

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the superclass of MouseEvent and KeyEvent? What is this class’ superclass?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Can a button fire a MouseEvent?
  2. Can a button fire a KeyEvent?
  3. Can a button fire an ActionEvent?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In which package are the main event classes located?

A

They are included in the java.awt.event package, except ListSelectionEvent and ChangeEvent, which are in the javax.swing.event package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an event listener?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you register a listener object by a source object?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

You can use the method getSource(), which returns the source object for the event.
E.g. e.getSource()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an inner class?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you use the methods and data defined in the out class, inside the inner class?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create an instance of the inner class?

A
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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it helpful to have inner classes?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an anonymous inner class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the syntax for creating an anonymous inner class? (Let’s say you wanted to add an event listener to the JButton jbtShrink)

A
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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Typical:
JFrame frame = new JFrame();
frame.pack();

What does the method pack() do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the perferred way of defining listener classes?

A

Using an inner class or an anonymous inner class is perferred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When defining a listener class, when should you use a regular inner class, and when should you use an anonymous inner class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which methods does the MouseListener interface contain for handling MouseEvent?

A

MouseListener contains the methods:

mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which methods does the MouseMotionListener interface contain for handling MouseEvent?

A

MouseMotionListener contains the methods:

mouseDragged, mouseMoved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a listener interface adapter?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Why does the ActionListener interface have no listener interface adapter?

A

Because ActionListener only contains one method: actionPerformed.

22
Q

When is a KeyEvent fired?

A

A KeyEvent is fired whenever a key is pressed, released, or typed (typed = pressed AND released) on a component.

23
Q

What methods does the KeyListener interface contain?

A

The KeyListener interface contains keyPressed, keyReleased, and keyTyped.

24
Q

How do you make a component focusable, and why is it useful to do so?

A

You can set a component as focusable by
componentObject.setFocusable(true);
You use the setFocusable method.
Only focused components can receive KeyEvent; you need to make a component focusable if you need it to fire key events.

25
Q

How can you refocus a component that is no longer focused?

A

You can use the method requestFocusInWindow(). E.g. componentName.requestFocusInWindow();

26
Q

What is a Timer object?

A

A Timer is a source object that fires ActionEvent at a fixed rate. Its sole objective is to serve as the source of an ActionEvent. It can be used to control animations.

27
Q

Describe the Timer class’ constructor(s).

A

The Timer class has only one constructor, which takes two parameters: an int “delay” that specifies the delay in millisecond between ActionEvents, and an ActionListener.

28
Q

What are the 4 methods of the Timer class?

A

addActionListener(listener: ActionListener): Adds additional ActionListeners.
start(): Starts this timer.
stop(): Stops this timer
setDelay(delay: int): Sets a new delay value for this timer.

29
Q
Pressing a button in a window (like a JButton) generates a(n) \_\_\_ event.
A. ItemEvent
B. MouseEvent
C. MouseMotionEvent
D. ActionEvent
E. ContainerEvent
A

The correct answer is

D. ActionEvent

30
Q
Clicking the closing button on the upper-right corner of a frame generates a(n) \_\_\_ event.
A. ItemEvent
B. WindowEvent
C. MouseMotionEvent
D. ComponentEvent
E. ContainerEvent
A

The correct answer is

B. WindowEvent

31
Q

Which of the following statements are true?
A. If a component can generate an event, any subclass of the component can generate the same type of event.
B. All the event classes are subclasses of EventObject.
C. A component on which an event is generated is called the source object.
D. Every GUI component can generate MouseEvent, KeyEvent, FocusEvent, and ComponentEvent.

A

All are true

32
Q
The component that processes the event is called \_\_\_\_.
A. the source object
B. the listener object
C. the adapter object
D. the adaptee object
A

The correct answer is

B. the listener object

33
Q

Which of the following statements are true?
A. Each event class has a corresponding listener interface.
B. The listener object’s class must implement the corresponding event-listener interface.
C. A source may have multiple listeners.
D. The listener object must be registered by the source object.
E. A listener may listen for multiple sources.

A

All are true.

34
Q
The interface \_\_\_ should be implemented to listen for a button action event.
A. MouseListener
B. ActionListener
C. FocusListener
D. WindowListener
E. ContainerListener
A

The correct answer is

B. ActionListener

35
Q
The method in the ActionEvent \_\_\_ returns the action command of the button.
A. getActionCommand()
B. getModifiers()
C. paramString()
D. getID()
A

The correct answer is

A. getActionCommand()

36
Q
The handler (e.g., actionPerformed) is a method in \_\_\_.
A. a source object 
B. a listener object
C. both source and listener object
D. the Object class
E. the EventObject class
A

The correct answer is
B. a listener object
Really, the method is defined as an abstract method in the listener interface, and implemented in the listener class from which the listener object is created.

37
Q
Every event object has the \_\_\_\_\_\_\_\_ method.
A. getSource()
B. getActionCommand()
C. getTimeStamp()
D. getWhen()
E. getKeyChar()
A

The correct answer is

A. getSource()

38
Q
Which of the following statements are true?
A. Inner classes can make programs simple and concise. 
B. An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. 
C. An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
D. An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
A

All are true.

39
Q

Which statement is true about a non-static inner class?
A. It must implement an interface.
B. It is accessible from any other class.
C. It can only be instantiated in the enclosing class.
D. It must be final if it is declared in a method scope.
E. It can access private instance variables in the enclosing object.

A

The correct answer is

E. It can access private instance variables in the enclosing object.

40
Q
Which of the following statements are true?
A. An anonymous inner class is an inner class without a name.
B. An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. 
C. An anonymous inner class must implement all the abstract methods in the superclass or in the interface.
D. An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object().
E. An anonymous inner class is compiled into a class named OuterClassName$n.class, where n is 1 if the anonymous inner class is the first of its kind in the outer class, 2 if the anonymous inner class is the second of its kind in the outer class etc.
A

All are true.

41
Q

Analyze the following code.

import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class Test extends JFrame implements ActionListener  {
  public Test() {
    JButton jbtOK = new JButton("OK");
    getContentPane().add(jbtOK);
  }

public void actionPerformed(ActionEvent e) {
System.out.println(“The OK button is clicked”);
}

public static void main(String[] args) {
JFrame frame = new Test();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

A. The program has a compile error because no listeners are registered with jbtOK.
B. The program has a runtime error because no listeners are registered with jbtOK.
C. The message “The OK button is clicked” is displayed when you click the OK button.
D. The actionPerformed method is not executed when you click the OK button, because no instance of Test is registered with jbtOK.
E. None of the above.

A

The correct answer is
D. The actionPerformed method is not executed when you click the OK button, because no instance of Test is registered with jbtOK.

42
Q

Analyze the following code.

import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class Test extends JFrame {
  public Test() {
    JButton jbtOK = new JButton("OK");
    JButton jbtCancel = new JButton("Cancel");
    getContentPane().add(jbtOK);
    getContentPane().add(jbtCancel);
    jbtOK.addActionListener(this);
    jbtCancel.addActionListener(this);
  }

public void actionperformed(ActionEvent e) {
System.out.println(“A button is clicked”);
}

public static void main(String[] args) {
JFrame frame = new Test();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

A. The program has compile errors on Lines 11 and 12 because Test does not implement ActionListener.
B. The program has compile errors on Line 15 because the signature of actionperformed is wrong.
C. The program has compile errors on Line 20 because new Test() is assigned to frame (a variable of JFrame).
D. The program has runtime errors on Lines 9 and 10 because jbtOK and jbtCancel are added to the same location in the container.
E. None of the above.

A

The correct answer is
A. The program has compile errors on Lines 11 and 12 because Test does not implement ActionListener.
If Test implements ActionListener, this program would work as planned, but this is NOT the preferred way of implementing event handling.

43
Q
To detect whether the right button of the mouse is pressed, you use the method \_\_\_\_ in the MouseEvent object evt.
A. evt.isAltDown()
B. evt.isControlDown()
C. evt.isMetaDown()
D. evt.isShiftDown()
A

The correct answer is

C. evt.isMetaDown()

44
Q

To listen to mouse clicked events, the listener must implement the ___ interface or extend the ___ adapter.
A. MouseListener/MouseAdapter
B. MouseMotionListener/MouseMotionAdapter
C. WindowListener/WindowAdapter
D. ComponentListener/ComponentAdapter

A

The correct answer is

A. MouseListener/MouseAdapter

45
Q
To get the x coordinate of the mouse pointer for the MouseEvent evt, you use \_\_\_.
A. evt.getX()
B. evt.getPoint().x
C. Either A or B
D. Neither A nor B
A

The correct answer is

C. Either A or B

46
Q
Which of the following are correct names for listener adapters?
A. ActionAdapter
B. MouseAdapter
C. KeyAdapter
D. WindowAdapter
A

All but A are correct.

47
Q
To listen to keyboard actions, the listener must implement the \_\_\_ interface or extend the \_\_\_ class.
A. MouseListener/MouseAdapter
B. KeyListener/KeyAdapter
C. WindowListener/WindowAdapter
D. ComponentListener/ComponentAdapter
A

The correct answer is

B. KeyListener/KeyAdapter

48
Q

Which of the following statements are true?
A. The keyPressed handler is invoked when a key is pressed.
B. The keyReleased handler is invoked when a key is released.
C. The keyTyped handler is invoked when a key is entered.
D. The keyTyped handler is invoked when a Unicode character is entered.

A

All but C are true.

49
Q

To enable a component to listen to keyboard events, you need to ___
A. Implement the KeyListener interface for the component.
B. Do componentName.setFocusable(true).
C. Invoke the component’s requestFocus method to set focus on this component.

A

You need to do all – A, B and C.

50
Q

What’s the difference between
componentName.requestFocus()
and
componentName.requestFocusInWindow()?

A

requestFocus gives the component focus, even if that means changing window/bringing the component’s window to the front.
requstFocusInWindow only gives the component focus if its window has the focus.

51
Q
Which of the following statements are true?
A. You can add a listener in the Timer constructor.
B. You can use the addActionListener method in the Timer class to add a listener.
C. You can specify a delay in the Timer constructor.
D. You can specify a delay using the setDelay method.
A

All are true.

52
Q

Which of the following statements are true?
A. You must always specify a listener when creating a Timer object.
B. You can add multiple listeners for a Timer object.
C. To stop a timer, invoke timer.stop().
D. To start a timer, invoke timer.start().
E. When a timer is created, it is automatically started.

A

All but E are true.