S
Steven J Sobol
I'm creating a control (subclass of JTextField) that handles input of IP
addresses* and am using a KeyAdapter to restrict input to numeric digits and
the period key.
It works really well. Too well, in fact, as keystrokes like backspace that
aren't valid input characters (but need to be passed) get eaten.
Is there a way to specify the set of non-alphanumeric keyboard keys without
having to specify each one's virtual keycode separately? I want to eat
keystrokes for alphanumeric/symbol keys other than 1234567890 and . but if
a key is pressed that is not an alphanumeric or symbol key (F1-F12, PgUp,
PgDn, Enter, Backspace, etc.) I don't want to do anything with it.
Here's my current code:
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String validChars="0123456789." ;
if ( validChars.indexOf(e.getKeyChar())==-1 ) {
e.consume();
}
}
});
TIA for whatever advice you can offer.
--
JustThe.net Internet & New Media Services, Apple Valley, CA PGP: 0xE3AE35ED
Steven J. Sobol, Geek In Charge / 888.480.4NET (4638) / (e-mail address removed)
Domain Names, $9.95/yr, 24x7 service: http://DomainNames.JustThe.net/
"someone once called me a sofa, but i didn't feel compelled to rush out and buy
slip covers." -adam brower * Hiroshima '45, Chernobyl '86, Windows 98/2000/2003
addresses* and am using a KeyAdapter to restrict input to numeric digits and
the period key.
It works really well. Too well, in fact, as keystrokes like backspace that
aren't valid input characters (but need to be passed) get eaten.
Is there a way to specify the set of non-alphanumeric keyboard keys without
having to specify each one's virtual keycode separately? I want to eat
keystrokes for alphanumeric/symbol keys other than 1234567890 and . but if
a key is pressed that is not an alphanumeric or symbol key (F1-F12, PgUp,
PgDn, Enter, Backspace, etc.) I don't want to do anything with it.
Here's my current code:
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String validChars="0123456789." ;
if ( validChars.indexOf(e.getKeyChar())==-1 ) {
e.consume();
}
}
});
TIA for whatever advice you can offer.
--
JustThe.net Internet & New Media Services, Apple Valley, CA PGP: 0xE3AE35ED
Steven J. Sobol, Geek In Charge / 888.480.4NET (4638) / (e-mail address removed)
Domain Names, $9.95/yr, 24x7 service: http://DomainNames.JustThe.net/
"someone once called me a sofa, but i didn't feel compelled to rush out and buy
slip covers." -adam brower * Hiroshima '45, Chernobyl '86, Windows 98/2000/2003