Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2012, 08:04 PM   PM User | #1
d.i.l.l.1990
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
d.i.l.l.1990 is an unknown quantity at this point
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.
d.i.l.l.1990 is offline   Reply With Quote
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,658
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
Reply

Bookmarks

Tags
hooks, java, keyboard

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:50 PM.


Advertisement
Log in to turn off these ads.