devxlogo

Creating a LIST_SELECT Event

Creating a LIST_SELECT Event

Question:
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,?)

Answer:
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”);                add(l);        }        // 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();                a.init();                a.start();                 f.setLayout(new BorderLayout());                f.add(“Center”, a);                f.pack();                f.show();        }}
When you run this program/applet, clicking on say, item 4, in the list causes the following output:
target = java.awt.List[5,5,136×75,selected=Item 4] when = 0id = 701x = 5y = 5key = 0modifiers = 0clickCount = 0arg = 3evt = 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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist