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;       }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: