...

View Full Version : hooks keyboard browser (java)



d.i.l.l.1990
02-27-2012, 09:04 PM
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.

Fou-Lu
02-27-2012, 10:41 PM
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
{
try
{
Integer.parseInt(str);
super.replace(fb, offs, length, str, a);
}
catch (NumberFormatException ex)
{
// Don't care, just abandon it.
}
}
});
}
f.add(field);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum