View Single Post
Old 02-27-2012, 09:41 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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)
        {
            ((
AbstractDocumentfield.getDocument()).setDocumentFilter(new DocumentFilter()
            {
                public 
void replace(FilterBypass fbint offsint lengthString strAttributeSet athrows BadLocationException
                
{
                    try
                    {
                        
Integer.parseInt(str);
                        
super.replace(fboffslengthstra);
                    }
                    catch (
NumberFormatException ex)
                    {
                        
// Don't care, just abandon it.
                    
}
                }
            });
        }
        
f.add(field);
        
f.pack();
        
f.setVisible(true);
        
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } 
Fou-Lu is offline   Reply With Quote