Jump to content

Event Listeners

75% developed
From Wikibooks, open books for an open world

Navigate User Interface topic:v  d  e )


An Event Listener, once set to an applet object, waits for some action to be performed on it, be it mouse click, mouse hover, pressing of keys, click of button, etc. The class you are using (e.g. JButton, etc.) reports the activity to a class set by the class using it. That method then decides on how to react because of that action, usually with a series of if statements to determine which action it was performed on. source.getSource() will return the name of the object that the event was performed on, while the source is the object passed to the function when the action is performed. Every single time the action is performed, it calls the method.

ActionListener

[edit | edit source]

ActionListener is an interface that could be implemented in order to determine how a certain event should be handled. When implementing an interface, all methods in that interface should be implemented, ActionListener interface has one method to implement named actionPerformed().

The code listing 9.6 shows how to implement ActionListener:

Computer code Code listing 9.6: EventApplet.java
import java.applet.Applet;
import java.awt.Button;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventApplet extends Applet {

    /**
     * Init.
     */
    public void init() {
        Button clickMeButton = new Button("Click me");

        final Applet eventApplet = this;

        ActionListener specificClassToPerformButtonAction = new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                Dialog dialog = new Dialog(getParentFrame(eventApplet), false);
                dialog.setLayout(new FlowLayout());
                dialog.add(new Label("Hi!!!"));
                dialog.pack();
                dialog.setLocation(100, 100);
                dialog.setVisible(true);
            }

            private Frame getParentFrame(Container container) {
                if (container == null) {
                    return null;
                } else if (container instanceof Frame) {
                    return (Frame) container;
                } else {
                    return getParentFrame(container.getParent());
                }

            }
        };
        clickMeButton.addActionListener(specificClassToPerformButtonAction);

        add(clickMeButton);
    }
}

When you compile and run the above code, the message "Hi!!!" will appear when you click on the button.

MouseListener

[edit | edit source]

Applet mouse listener does not differ from the AWT mouse listener in general. When the mouse is in the applet area, the listener receives notifications about the mouse clicks and drags (if MouseListener is registered) and mouse movements (if MouseMotionListener is registered). As applets are often small, it is a common practice to let applet itself to implement the mouse listeners.


Clipboard

To do:
Add some exercises like the ones in Variables