For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > How to catch mouse events from java.awt.TextField









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to catch mouse events from java.awt.TextField
Daniel Tahin

2005-02-22, 4:02 pm

Hi!

I need your valuable help! I'm working long hours on it without any luck:-((
I want to catch mouse events from a TextField object, that i extended.
But it seems i can't do that (however i can get keyboard events). And i
would like to do this using JDK 1.0 API.
What do i wrong?
Here is my test code (it consists a Frame obejct, that contains my
TextField obejct):



package p;

import java.awt.Frame;
import java.awt.Event;


public class Test extends Frame
{
private TextField inputfield;


public Test() {
super("Test frame...");
resize(200, 50);
move(40, 40);
inputfield = new TextField();
inputfield.resize(200, 50);
add(inputfield);
show();
}

public boolean handleEvent(Event e) {
if (e.target == this && e.id == Event.WINDOW_DESTROY) {
hide();
System.exit(1);
}
return super.handleEvent(e);
}

public boolean action(Event e, Object o) {
if(e.id == Event.ACTION_EVENT) {
if (e.target == inputfield) {
System.out.println("\n\nText from inputfield: "+(String)o);
inputfield.setText("");
}
}
return super.action(e, o);
}

/*public boolean postEvent(Event ev) {
System.out.println("Test: postEvent() - "+ev);
return super.postEvent(ev);
}*/

public static void main(String args[]) {
Test tmp = new Test();
}
}




package p;

import java.awt.Event;


public class TextField extends java.awt.TextField
{
//private Plugin pt1;


public TextField() {
super();
//pt1 = null;
//initPlugins();
}

/*private void initPlugins() {
//initialize pt1!
try{
pt1 = (Plugin)(Class.forName("p.Plugin")).newInstance();
pt1.doInit((Object)this);
}
catch(Exception e)
{
pt1 = null;
System.err.println("Error loading Plugin.class!");
e.printStackTrace();
}
}*/

public boolean handleEvent(Event e) {
System.out.println("\nTextField: handleEvent() - "+e);
if(e.target == this && e.id == Event.MOUSE_DOWN) {
System.out.println("TextField clicked...");
}
return super.handleEvent(e);
}

/*public boolean action(Event e, Object o) {
System.out.println("\nTextField: action() - "+e);
return super.action(e, o);
}*/
}



Thanx a lot for your hints!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com