devxlogo

Responding to Mousedown Event in TextField

Responding to Mousedown Event in TextField

Question:
How can I respond to a mousedown event in a TextField?

Answer:
From the Java 1.0.2 API specs:

“In Java 1.0, the AWT does not send mouse or focus events to a text area. In Java 1.1, the AWT sends the text area all mouse, keyboard, and focus events that occur over it.”
This means you would have to wait until Java 1.1 is released andimplemented in browsers like Netscape, HotJava, and InternetExplorer, or implement a custom TextField widget that accomplishedwhat you wanted.

I wrote the following little applet just to convince myself thatTextFields don’t get MOUSE_DOWN events; and sure enough, theshowStatus() method is never called.

import java.applet.*;import java.awt.*;import java.util.*;public class MouseText extends Applet {       public void init() {               Panel namePanel = new Panel();               Label nameLabel = new Label(“User name: “);               namePanel.add(nameLabel);               TextField username = new TextField(20);               namePanel.add(username);               add(namePanel);       }       public synchronized boolean handleEvent(Event e) {               if (e.target instanceof TextField &&                       e.id == Event.MOUSE_DOWN) {                       showStatus(“MouseDown at: ” + new Date() );               }               return false;       }}

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