Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: User Interface Design
Expertise: Beginner
Mar 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Creating a LIST_SELECT Event

How do I create a LIST_SELECT event? Specifically, what should I put in the fields of the statement:
Event anevent = new Event(mylst, event.when, 
        event.LIST_SELECT, ?, ? event.key, event.modifiers,?)

Java's Event class has three constructors which allow you to create Events with various degrees of specifics:

Event(Object, long, int, int, int, int, int, Object)
Constructs an event with the specified target component, time stamp, event type, x and y coordinates, keyboard key, state of the modifier keys and argument.
Event(Object, long, int, int, int, int, int)
Constructs an event with the specified target component, time stamp, event type, x and y coordinates, keyboard key, state of the modifier keys and an argument set to null.
Event(Object, int, Object)
Constructs an event with the specified target component, event type, and argument.

The last one allows you to create an event without having to specify the other parameters; Java will fill them in with appropriate defaults. For example, the x and y coordinates, or the keyboard key don't really make sense for the LIST_SELECT event, so you're probably better off letting Java fill them in for you by calling:

        Event anEvent = new Event(mylist, Event.LIST_SELECT, null);
If on the other hand, you absolutely have to set these parameters, you can take apart LIST_SELECT events and see what default values Java uses for the other parameters. The following applet does precisely that:

import java.awt.*;
import java.io.*;
import java.applet.*;
public class ListSelect extends Applet {  
        // Create a List with some items in it
        public void init() {
                List l = new List(4, false);
                l.addItem("Item 1");
                l.addItem("Item 2");
                l.addItem("Item 3");
                l.addItem("Item 4");
                l.addItem("Item 5");
                l.addItem("Item 6");
                l.addItem("Item 7");
                l.addItem("Item 8");

        // print the contents of a LIST_SELECT event 
        public boolean handleEvent(Event e) {
                if (e.id == Event.LIST_SELECT) {
                        System.out.println("target = " + e.target);
                        System.out.println("when = " + e.when);
                        System.out.println("id = " + e.id);
                        System.out.println("x = " + e.x);
                        System.out.println("y = " + e.y);
                        System.out.println("key = " + e.key);
                        System.out.println("modifiers = " + e.modifiers);
                        System.out.println("clickCount = " + e.clickCount);
                        System.out.println("arg = " + e.arg);
                        System.out.println("evt = " + e.evt);
                return false;
        // Defining a main() routine allows the applet to be
        // run as a standalone Java application
        public static void main(String argv[]) {
                Frame f = new Frame("ListSelect");  
                Applet a = new ListSelect();
                f.setLayout(new BorderLayout());
                f.add("Center", a);

When you run this program/applet, clicking on say, item 4, in the list causes the following output:
target = java.awt.List[5,5,136x75,selected=Item 4] when = 0
id = 701
x = 5
y = 5
key = 0
modifiers = 0
clickCount = 0
arg = 3
evt = null
As you can see, the parameters are all set to zero except for the arg, and the x and y coordinates. The arg is the number of the item selected (numbering starts at zero), and the x and y are arbitrarily set to 5. This behavior may of course be different from platform to platform, so you'll have to test your code on several platforms to make sure your program works correctly everywhere.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date