Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: hooks keyboard browser (java)
02-27-2012, 09:04 PM #1
- Join Date
- Feb 2012
- Thanked 0 Times in 0 Posts
hooks keyboard browser (java)
I have an input field that i want to enter only numbers. There is a way to block all other keys except number keys and numpad key? Because i don't know java, can anyone know a good example about it, or something to search in google? I found some links, but i don't understand a lot. Thanks in advance.
02-27-2012, 10:41 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,668 Times in 2,637 Posts
You can listen for the events and respond accordingly, but its probably easier to just set the DocumentFilter. Here's a quick example using an anonymous class:
public static void main(String... argv)
JFrame f = new JFrame();
JTextField field = new JTextField();
if (field.getDocument() instanceof AbstractDocument)
((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentFilter()
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException
super.replace(fb, offs, length, str, a);
catch (NumberFormatException ex)
// Don't care, just abandon it.