Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8102198

Provide some utility properties on TextField/TextInputControl for restrictung user input

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: javafx

      Description

      It would be useful, if TextField/TextInputControl had some properties, which allow the developer to restrict the user input.

      e.g. some useful properties would be:

      maxLength (user cannot input more than X characters, as it exists in HTML).
      restrict (as it exists in Flex: http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Text_11.html )

      Please also consider, that input can happen by context menu and CTRL+V, too (do not rely on KeyEvents).


      Sample:


      import javafx.beans.property.IntegerProperty;
      import javafx.beans.property.SimpleIntegerProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.scene.control.TextField;

      /**
       * A text field, which restricts the user's input.
       */
      public class AdvancedTextField extends TextField {

          private StringProperty restrict = new SimpleStringProperty();

          private IntegerProperty maxLength = new SimpleIntegerProperty(-1);

          public int getMaxLength() {
              return maxLength.get();
          }

          /**
           * Sets the max length of the text field.
           *
           * @param maxLength The max length.
           */
          public void setMaxLength(int maxLength) {
              this.maxLength.set(maxLength);
          }

          /**
           * Sets a regular expression character class which restricts the user input.<br/>
           * E.g. [0-9] only allows numeric values.
           *
           * @param restrict The regular expression.
           */
          public void setRestrict(String restrict) {
              this.restrict.set(restrict);
          }

          public String getRestrict() {
              return restrict.get();
          }

          public StringProperty restrictProperty() {
              return restrict;
          }

          public IntegerProperty maxLengthProperty() {
              return maxLength;
          }

          public AdvancedTextField() {

              textProperty().addListener(new ChangeListener<String>() {

                  private boolean ignore;

                  @Override
                  public void changed(ObservableValue<? extends String> observableValue, String s, String s1) {
                      if (ignore)
                          return;
                      if (maxLength.get() > -1 && s1.length() > maxLength.get()) {
                          ignore = true;
                          setText(s1.substring(0, maxLength.get()));
                          ignore = false;
                      }

                      if (restrict.get() != null && !restrict.get().equals("") && !s1.matches(restrict.get() + "*")) {
                          ignore = true;
                          setText(s);
                          ignore = false;
                      }
                  }
              });
          }
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                leifs Leif Samuelsson (Inactive)
                Reporter:
                cschudtjfx Christian Schudt (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported: