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:
PHP Code:
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);
}