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) { } } }); 
7
  • Write a listener that reads TextField's text, and get rid of letters. Commented Nov 26, 2013 at 10:52
  • Example? I have very basic knowledge for JavaFX? Commented Nov 26, 2013 at 10:53
  • What about this Integer.parseInt(fieldNport.getText())? Commented Nov 26, 2013 at 10:55
  • TextField.text is a property, it means that you can add your listeners directly on this property. So you have something along the lines of : fieldNPort.text.addListener(new Event....) Do you know listeners and event in Java? Commented Nov 26, 2013 at 10:56
  • Can I use Integer.parseInt(fieldNport.getText()) or it's not recommended? Commented Nov 26, 2013 at 10:57

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

I updated the code again. Can you recommend me some improvement of the code?
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.
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 : stackoverflow.com/questions/10372862/…
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.