I have two text boxes in my Awt program. For each text box the code inside keypress is different. For the first text box it has to check the user entered values which must be only digits and for the second i have to check that the user enters only characters. If this is the situation how can I handle events ?? I am able to do creating anonymous classes. but in this case i have to craete the interface twice using new KeyListener().If i have say, some 10 text boxes I think this is tedious ??? There must be some other way which i am not able to figure out ??? I tried implemnting the inteface but iam not knowing how to say for which textbox the following code must fire on it's keypress event??? Is there a way to do this???
Questions like this have come up before - see a related thread here. We still don't have a good ansewr though - I keep hoping someone will show up with a solution. If you do eventually get this to work, I'd appreciate if you could come back afterwards and show us what you did. It sounds like you're headed in the right general direction though, so I'll try offering some suggestions. If you're doing this for a lot of different textboxes, it will probably be worthwhile to put most of the behviour you want into a single new class ("ShansImprovedTextBox" or something) and then create subclasses from this as necessary. Hmmm... what I'd like someone to do is make a class that accepts arguments which defines the number and type of characters which can be input. Like:
So to create a new field for a 7-digit phone number, you'd say <code><pre> XTextField tf1 = new XTextField(XTextField.NUMERIC, 7);</pre></code> Or for a password that can only be 10 characters long, use <code><pre> XTextField tf2 = new MaskedTextField(XTextField.ALPHANUMERIC, 10);</pre></code> This still leaves the issue of writing the class itself, which is not easy - but the idea is, once you have done it once, you could use it for lots of different things, easily. For your last question - you probably need the listener to have access to a reference to the extended TextField class you are creating. There are many ways to do this - if you define the listener as an inner class inside XTextField, then you can use "XTextField.this" to refer to the XTF instance from within the listener (since "this" by itself would refer to the inner listener class). Good luck!
[This message has been edited by Jim Yingst (edited March 16, 2000).]