2

I have this field in which I insert port number. I would like to convert the string automatically into number:

fieldNport = new TextField();
    fieldNport.setPrefSize(180, 24);
    fieldNport.setFont(Font.font("Tahoma", 11));
    grid.add(fieldNport, 1, 1);

Can you tell how I can do this? I cannot find suitable example in stack overflow.

EDIT:

Maybe this:

 fieldNport.textProperty().addListener(new ChangeListener()
        {
            @Override
            public void changed(ObservableValue o, Object oldVal, Object newVal)
            {
                try
                {
                    int Nport = Integer.parseInt((String) oldVal);
                }
                catch (NumberFormatException e)
                {

                }
            }
        });
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

5

Starting with JavaFX 8u40, you can set a TextFormatter object on a text field:

UnaryOperator<Change> filter = change -> {
    String text = change.getText();

    if (text.matches("[0-9]*")) {
        return change;
    }

    return null;
};
TextFormatter<String> textFormatter = new TextFormatter<>(filter);
fieldNport = new TextField();
fieldNport.setTextFormatter(textFormatter);

This avoids both subclassing and duplicate change events that you will get when you add a change listener to the text property and modify the text in that listener.

Uwe
  • 844
  • 8
  • 13
2

You can write something like this :

fieldNPort.text.addListener(new ChangeListener(){
        @Override public void changed(ObservableValue o,Object oldVal, Object newVal){
             //Some Code
             //Here you can use Integer.parseInt methods inside a try/catch 
             //because parseInt throws Exceptions
        }
      });

Here are all the things you'd need about properties and Listeners in JavaFX:
http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
If you have any question, I'll be glad to help.

Fabinout
  • 878
  • 7
  • 25
  • I updated the code again. Can you recommend me some improvement of the code? – Peter Penzov Nov 26 '13 at 11:21
  • Now you have to understand that the try/catch part of your code will be executed EVERY time the text property is changed by the user. – Fabinout Nov 26 '13 at 11:25
  • You should not correct this. Because this is what you're trying to achieve, you let people write what they want and you filter it. If Integer.parseInt(string) works, it means that the string is made of digits. If it throws an exception, you must use a Java method to remove every non-digit character. For example : http://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters – Fabinout Nov 26 '13 at 11:35
2

Maybe this is what you need:

fieldNPort= new TextField()
    {
        @Override
        public void replaceText(int start, int end, String text)
        {
            if (text.matches("[0-9]*"))
            {
                super.replaceText(start, end, text);
            }
        }

        @Override
        public void replaceSelection(String text)
        {
            if (text.matches("[0-9]*"))
            {
                super.replaceSelection(text);
            }
        }
    };

This will restrict the users from entering anything but numbers(you can modify the regex expression to your needs) and then you do not have to worry about Integer.parseInt throwing any exception.

Aspirant
  • 1,934
  • 4
  • 25
  • 44