Click to See Complete Forum and Search --> : [Q] Event notifications for dialog components


MikeX
January 22nd, 1999, 09:27 AM
Greetings:


I am doing that "self-paced 'learn Java in my free time'" thing, with a MFC / C++ background.


Given a applet, I want to launch a dialog upon some event. I have a compilable dialog class which owns all it's controls. I create a Panel and add() all TextFields and Labels to it, then add() the Panel to the dialog. After a manual resize() and newing a LayoutManager, I'm good to go. (I have example code for a "quit" button and it's handling in Dialog.action().)


The issues:


I would like to A) trap all TextField keystroke events (Event.key ) for each TextField (Event.target.) The problem seems to be that no TextField events post "messages" to the Dialog.action() so I don't get the change to do data validation.


Does the Panel get these "messages"? If not, how do I catch TextField events (without extending a class for each TextField)? Where have I missed the architurial-boat?


Mike X

MikeX@Bellsouth.net

July 4th, 1999, 08:12 AM
First of all u can add components like textfield to a dialog without requiring a panel,but nothing wrong to add it to a panel.Then as java.awt.Dialog can respond to TextEvents because it inherits from Component.textFields generate texEvents first ! Now after this essential prequiste,ur dialog class can implement TextListener(a interface and implement textValueChangedmethod in Dialog class) and add textfield.addTextListener(this) which means that Dialog is text listener for textfield.In ur textvaluechanged event u can add ur code,but verify the texfield that has generated the event(event.getSource() and verify the return value with textfield object).The concept is textfield generates a event and u can make a class to listen which can be class that the textfield is a atrribute or some other class too.This can be extended to any awt component.Hope this helps and good luck

scotty99
July 4th, 1999, 10:09 AM
Consider this:
TextField tf1,tf2
public boolean keyDown(Event e, int key){
if (e.target==tf1)
//keyhandling code for tf1 here
if (e.target==tf2)
//tf2 keyhandling code here
}
This will work, I have used it in dialog classes.
If you return TRUE for a condition in the keyDown subroutine then of course the event won't post to dialog.action.
Example:
if (evt.target==tf1){
if (key<48 || key>57) //not number keys
return true;
else
return false;
}
The result is only numbers can be typed in your textfield. If another key is hit it won't appear in the textfield and
of course won't post an event to dialog.action
I use the keyDown for my textfields and never use action for any textfield processing.