7 Replies Latest reply on Mar 12, 2012 6:06 PM by jsmith

    How to implement InputFilter for TextField

    omurata
      hello everyone

      I write my own Input Filter class extending PlainDocument or DocumentFilter when I use Swing

      How to implement customized Input Filter for TextField in JavaFX ?

      Best regards.
      Tadashi Ohmura
        • 1. Re: How to implement InputFilter for TextField
          john16384
          This is pretty crude, but it works... you'll need to work it further a bit to make it a bit more user friendly.
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;
          
          import javafx.application.Application;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          import javafx.scene.Scene;
          import javafx.scene.control.TextField;
          import javafx.stage.Stage;
          
          public class InputFilterTest extends Application {
            private static final Pattern INPUT_PATTERN = Pattern.compile("[0-9]{0,4}[A-Z]{0,2}");
          
            @Override
            public void start(Stage stage) throws Exception {
              final TextField label = new TextField() {{
                textProperty().addListener(new ChangeListener<String>() {
                  @Override
                  public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    Matcher matcher = INPUT_PATTERN.matcher(newValue);
          
                    if(!matcher.matches()) {
                      setText(oldValue);
                    }
                  }
          
                });
              }};
          
              stage.setScene(new Scene(label));
              stage.show();
            }
          
            public static void main(String[] args) {
              Application.launch(args);
            }
          
          }
          • 2. Re: How to implement InputFilter for TextField
            omurata
            Thank you John

            What about my code ?
            import javafx.application.Application;
            import javafx.scene.Scene;
            import javafx.stage.Stage;
            import javafx.scene.control.TextField;
            import javafx.scene.layout.VBox;
            import java.util.regex.Matcher;
            import java.util.regex.Pattern;
            
            public class TextField04A extends Application {
            
              public void start(Stage stage) throws Exception {
                VBox root = new VBox();
                root.setSpacing(10);
                Scene scene = new Scene( root );
            
                MyTextField textField = new MyTextField();
                root.getChildren().addAll( textField );
            
                stage.setScene(scene);
                stage.setTitle("TextField04A");
                stage.setX(300); stage.setY(200); stage.setWidth(300); stage.setHeight(320);
                stage.show();
              }
              //------------------------------------------------
              class MyTextField extends TextField {
                Pattern intPattern = Pattern.compile("[0-9]"); 
                public void replaceText(int start, int end, String text) {
                  Matcher match = intPattern.matcher(text);
                  if( match.matches() || start!=end ){
                    super.replaceText( start, end, text );
                  }
                }
                public void replaceSelection(String text) {
                  Matcher match = intPattern.matcher(text);
                  if( match.matches() ){
                    super.replaceSelection( text );
                  }
                }
              }
              //-----------------------------------------------
              //
              public static void main(String[] args) {
                launch(args);
              }
            }
            • 3. Re: How to implement InputFilter for TextField
              john16384
              Yes, that's a different way of doing it by subclassing the TextField. You could make a reusable FormattedTextField like this I think.

              I like your approach better :)

              Edited by: john16384 on Mar 11, 2012 8:20 PM
              • 4. Re: How to implement InputFilter for TextField
                omurata
                Thank you John.

                Your sample code helped me very much.
                • 5. Re: How to implement InputFilter for TextField
                  Richard Bair-Oracle
                  You might find some of the controls on https://github.com/fxexperience/code useful, including an IntegerField. I thought I had blogged about them but it looks like I only blogged about the MoneyField.
                  • 6. Re: How to implement InputFilter for TextField
                    omurata
                    Thank you Richard

                    FXExperienceControls is an interesting project.
                    • 7. Re: How to implement InputFilter for TextField
                      jsmith
                      Here is another example of different methods of limiting values in an input field => https://gist.github.com/1962045