0

I was browsing here on StackOverflow a solution to a problem: essentially I want the TextField to limit user input to just numeric values/ integers. I have found an answer that mentions a listener:

public class CubeController {

  public Label areaCube, volumeCube;
  public TextField insertValue;

  public void checkValue(){
  insertValue.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
      if (!newValue.matches("\\d{0,7}([\\.]\\d{0,4})?")) {
        insertValue.setText(oldValue);
      }
    }
  });}

}

I have tried to implement it on a TextField but with no success. (Initialized in an FXML file) What exactly is a Listener, and why it seems to not have effect on my TextField ? Thanks to everybody.

<TextField onAction="#checkValue" fx:id="insertValue" layoutX="269.0" layoutY="178.0" promptText="Insert edge measure" />
Babakaman
  • 27
  • 1
Loader
  • 1
  • 2
  • A "listener" is a implementation of the [observer pattern](https://www.oodesign.com/observer-pattern.html), which is very common. It allows one or more interested parties to "listen" to or "observer" changes to properties of a object – MadProgrammer Mar 23 '19 at 00:56
  • 1
    A [`TextFormatter`](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/control/TextFormatter.html) might better suit your needs. – Slaw Mar 23 '19 at 01:22
  • @Slaw thank you. How I can implement it ? For now I'm managing it throwing an alert message if the user try to perform action when he insert non-numeric values. – Loader Mar 23 '19 at 01:30
  • Might help: https://stackoverflow.com/questions/31039449/java-8-u40-textformatter-javafx-to-restrict-user-input-only-for-decimal-number and https://stackoverflow.com/questions/40472668/numeric-textfield-for-integers-in-javafx-8-with-textformatter-and-or-unaryoperat – Slaw Mar 23 '19 at 01:34
  • using a textFormatter (as @Slaw already suggested) is the _only_ suitable option (changing the value of a property - at the time of notification - back to its old value in a listener is strongly discouraged!) If you don't know how-to, there's its api doc plus tons of examples on the web :) – kleopatra Mar 23 '19 at 10:11
  • also: doing any manual pattern matching is rarely a good idea (because it will break with the next Locale), instead use a NumberFormat – kleopatra Mar 23 '19 at 10:12

1 Answers1

0

The purpose of a listener is to observe a certain value, in your case the text inside a text field. Whenever there is a change in the value of your text, like when you type something, the method changed will be called and you are given three parameters, the object you are observing, the previous value before being changed, and the new value after being changed. Now to make the listener you need to have a controller class and define what it should do when the observed value changes.

Here's a link on how you can get the textfield from fxml How to get Text from fxml TextField

alvinalvord
  • 394
  • 2
  • 11
  • Thanks @alvinalvord the first chunk of code is taken from the Controller and I gave actually an action to do in case the TextField doesn't contain numeric values. – Loader Mar 23 '19 at 00:56