tags:

views:

311

answers:

3

Hi, i want to create text field and textarea which should only take float value as input how can i do this in java?

A: 

I assume you know how to access the onKeyPress event... there you go!

Paulo Santos
+2  A: 

JFormattedTextField with a NumberFormatter for the textfield. I don't really see the use of doing this in a text area - how large of a floating point number are you expecting?

Nate
A: 

Depending on what you need, something along the lines of this may work for you:

myTextField.setInputVerifier(new InputVerifier() {

            @Override
            public boolean verify(JComponent input) {
                JTextField textField = ((JTextField) input);
                try {

                    Float isFloat = Float.valueOf(textField.getText());
                    textField.setBackground(Color.WHITE);
                    return true;
                } catch (NumberFormatException e) {
                    textField.setBackground(Color.RED);
                    return false;
                }

            }
        });
instanceofTom