Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: AWT
Expertise: Beginner
Mar 20, 1997

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 and implemented in browsers like Netscape, HotJava, and Internet Explorer, or implement a custom TextField widget that accomplished what you wanted.

I wrote the following little applet just to convince myself that TextFields don't get MOUSE_DOWN events; and sure enough, the showStatus() 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;
       }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

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