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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as