Warning: unlink(/home/adddocom/public_html/s2c/defaultlogic/yts/search/1/Java_ProgrammingAppletsEvent_Listeners.json): No such file or directory in /home/adddocom/public_html/s2/lib/mpr.php on line 691
Java Programming/Applets/Event Listeners Learning | Java Programming/Applets/Event Listeners Facts and Resources | DefaultLogic For Business




Java Programming/Applets/Event Listeners


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

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
 1 import java.applet.Applet;
 2 import java.awt.Button;
 3 import java.awt.Container;
 4 import java.awt.Dialog;
 5 import java.awt.FlowLayout;
 6 import java.awt.Frame;
 7 import java.awt.Label;
 8 import java.awt.event.ActionEvent;
 9 import java.awt.event.ActionListener;
10 
11 public class EventApplet extends Applet {
12 
13     /**
14      * Init.
15      */
16     public void init {
17         Button clickMeButton = new Button("Click me");
18 
19         final Applet eventApplet = this;
20 
21         ActionListener specificClassToPerformButtonAction = new ActionListener {
22 
23             public void actionPerformed(ActionEvent event) {
24                 Dialog dialog = new Dialog(getParentFrame(eventApplet), false);
25                 dialog.setLayout(new FlowLayout);
26                 dialog.add(new Label("Hi!!!"));
27                 dialog.pack;
28                 dialog.setLocation(100, 100);
29                 dialog.setVisible(true);
30             }
31 
32             private Frame getParentFrame(Container container) {
33                 if (container == null) {
34                     return null;
35                 } else if (container instanceof Frame) {
36                     return (Frame) container;
37                 } else {
38                     return getParentFrame(container.getParent);
39                 }
40 
41             }
42         };
43         clickMeButton.addActionListener(specificClassToPerformButtonAction);
44 
45         add(clickMeButton);
46     }
47 }

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

MouseListener

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


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Java_Programming/Applets/Event_Listeners
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at NCR Works. Create an account using LinkedIn to manage and organize your omni-channel knowledge. NCR Works is like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us