Java Code Examples for javafx.scene.control.TextField#setTextFormatter()

The following examples show how to use javafx.scene.control.TextField#setTextFormatter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidatorUtils.java    From kafka-message-tool with MIT License 7 votes vote down vote up
public static void configureTextFieldToAcceptOnlyDecimalValues(TextField textField) {

        DecimalFormat format = new DecimalFormat("#");

        final TextFormatter<Object> decimalTextFormatter = new TextFormatter<>(change -> {
            if (change.getControlNewText().isEmpty()) {
                return change;
            }
            ParsePosition parsePosition = new ParsePosition(0);
            Object object = format.parse(change.getControlNewText(), parsePosition);

            if (object == null || parsePosition.getIndex() < change.getControlNewText().length()) {
                return null;
            } else {
                return change;
            }
        });
        textField.setTextFormatter(decimalTextFormatter);
    }
 
Example 2
Source File: ChartViewPresenter.java    From mokka7 with Eclipse Public License 1.0 6 votes vote down vote up
private static void addNumericValidation(TextField field) {
    field.getProperties().put("vkType", "numeric");
    field.setTextFormatter(new TextFormatter<>(c -> {
        if (c.isContentChange()) {
            if (c.getControlNewText().length() == 0) {
                return c;
            }
            try {
                Integer.parseInt(c.getControlNewText());
                return c;
            } catch (NumberFormatException e) {
            }
            return null;

        }
        return c;
    }));
}
 
Example 3
Source File: ReadViewPresenter.java    From mokka7 with Eclipse Public License 1.0 6 votes vote down vote up
private static void addNumericValidation(TextField field) {
    field.getProperties().put("vkType", "numeric");
    field.setTextFormatter(new TextFormatter<>(c -> {
        if (c.isContentChange()) {
            if (c.getControlNewText().length() == 0) {
                return c;
            }
            try {
                Integer.parseInt(c.getControlNewText());
                return c;
            } catch (NumberFormatException e) {
            }
            return null;

        }
        return c;
    }));
}
 
Example 4
Source File: DoubleComponent.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public DoubleComponent(int inputsize, Double minimum, Double maximum, NumberFormat format) {
  this.minimum = minimum;
  this.maximum = maximum;

  textField = new TextField();
  textField.setTextFormatter(new TextFormatter<>(new NumberStringConverter(format)));
  textField.setPrefWidth(inputsize);

  // Add an input verifier if any bounds are specified.
  if (minimum != null || maximum != null) {
    // textField.setInputVerifier(new MinMaxVerifier());
  }

  getChildren().add(textField);
}
 
Example 5
Source File: TextFormatterUtils.java    From tcMenu with Apache License 2.0 5 votes vote down vote up
public static void applyIntegerFormatToField(TextField field) {
    field.setTextFormatter( new TextFormatter<>(c ->
    {
        if (c.getControlNewText().isEmpty()) {
            return c;
        }

        return (INTEGER_MATCH.matcher(c.getControlNewText()).matches()) ? c : null;
    }));
}
 
Example 6
Source File: TextManagedMenuItem.java    From tcMenu with Apache License 2.0 5 votes vote down vote up
public static void applyIpFormatting(TextField field) {
    field.setTextFormatter( new TextFormatter<>(c ->
    {
        if (c.getControlNewText().isEmpty()) {
            return c;
        }

        return (IPADDRESS.matcher(c.getControlNewText()).matches()) ? c : null;
    }));
}
 
Example 7
Source File: CombinedModuleSetHighlightDialog.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
public CombinedModuleSetHighlightDialog(@Nonnull Stage parent, @Nonnull CombinedModulePlot plot,
    @Nonnull String command) {

  desktop = MZmineCore.getDesktop();
  this.plot = plot;
  rangeType = command;
  mainPane = new DialogPane();
  mainScene = new Scene(mainPane);

  // Use main CSS
  mainScene.getStylesheets()
      .addAll(MZmineCore.getDesktop().getMainWindow().getScene().getStylesheets());
  setScene(mainScene);

  initOwner(parent);

  String title = "Highlight ";
  if (command.equals("HIGHLIGHT_PRECURSOR")) {
    title += "precursor m/z range";
    axis = plot.getXYPlot().getDomainAxis();
  } else if (command.equals("HIGHLIGHT_NEUTRALLOSS")) {
    title += "neutral loss m/z range";
    axis = plot.getXYPlot().getRangeAxis();
  }
  setTitle(title);

  Label lblMinMZ = new Label("Minimum m/z");
  Label lblMaxMZ = new Label("Maximum m/z");

  NumberStringConverter converter = new NumberStringConverter();

  fieldMinMZ = new TextField();
  fieldMinMZ.setTextFormatter(new TextFormatter<>(converter));
  fieldMaxMZ = new TextField();
  fieldMaxMZ.setTextFormatter(new TextFormatter<>(converter));

  pnlLabelsAndFields = new GridPane();
  pnlLabelsAndFields.setHgap(5);
  pnlLabelsAndFields.setVgap(10);

  int row = 0;
  pnlLabelsAndFields.add(lblMinMZ, 0, row);
  pnlLabelsAndFields.add(fieldMinMZ, 1, row);

  row++;
  pnlLabelsAndFields.add(lblMaxMZ, 0, row);
  pnlLabelsAndFields.add(fieldMaxMZ, 1, row);

  // Create buttons
  mainPane.getButtonTypes().add(ButtonType.OK);
  mainPane.getButtonTypes().add(ButtonType.CANCEL);

  btnOK = (Button) mainPane.lookupButton(ButtonType.OK);
  btnOK.setOnAction(e -> {
    if (highlightDataPoints()) {
      hide();
    }
  });
  btnCancel = (Button) mainPane.lookupButton(ButtonType.CANCEL);
  btnCancel.setOnAction(e -> hide());

  mainPane.setContent(pnlLabelsAndFields);

  sizeToScene();
  centerOnScreen();
  setResizable(false);

}
 
Example 8
Source File: ProductIonFilterSetHighlightDialog.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
public ProductIonFilterSetHighlightDialog(@Nonnull Stage parent, ProductIonFilterPlot plot,
    String command) {

  this.desktop = MZmineCore.getDesktop();
  this.plot = plot;
  this.rangeType = command;
  mainPane = new DialogPane();
  mainScene = new Scene(mainPane);
  mainScene.getStylesheets()
      .addAll(MZmineCore.getDesktop().getMainWindow().getScene().getStylesheets());
  setScene(mainScene);
  initOwner(parent);
  String title = "Highlight ";
  if (command.equals("HIGHLIGHT_PRECURSOR")) {
    title += "precursor m/z range";
    axis = plot.getXYPlot().getDomainAxis();
  } else if (command.equals("HIGHLIGHT_NEUTRALLOSS")) {
    title += "neutral loss m/z range";
    axis = plot.getXYPlot().getRangeAxis();
  }
  setTitle(title);

  Label lblMinMZ = new Label("Minimum m/z");
  Label lblMaxMZ = new Label("Maximum m/z");

  NumberStringConverter converter = new NumberStringConverter();

  fieldMinMZ = new TextField();
  fieldMinMZ.setTextFormatter(new TextFormatter<>(converter));
  fieldMaxMZ = new TextField();
  fieldMaxMZ.setTextFormatter(new TextFormatter<>(converter));

  pnlLabelsAndFields = new GridPane();
  pnlLabelsAndFields.setHgap(5);
  pnlLabelsAndFields.setVgap(10);

  int row = 0;
  pnlLabelsAndFields.add(lblMinMZ, 0, row);
  pnlLabelsAndFields.add(fieldMinMZ, 1, row);

  row++;
  pnlLabelsAndFields.add(lblMaxMZ, 0, row);
  pnlLabelsAndFields.add(fieldMaxMZ, 1, row);

  // Create buttons
  mainPane.getButtonTypes().add(ButtonType.OK);
  mainPane.getButtonTypes().add(ButtonType.CANCEL);

  btnOK = (Button) mainPane.lookupButton(ButtonType.OK);
  btnOK.setOnAction(e -> {
    if (highlightDataPoints()) {
      hide();
    }
  });
  btnCancel = (Button) mainPane.lookupButton(ButtonType.CANCEL);
  btnCancel.setOnAction(e -> hide());

  mainPane.setContent(pnlLabelsAndFields);

  sizeToScene();
  centerOnScreen();
  setResizable(false);
}
 
Example 9
Source File: Controller.java    From molecular-music-generator with MIT License 4 votes vote down vote up
public void setup(Stage stage) {
    directoryChooser = new DirectoryChooser();
    this.stage = stage;

    props = new Properties();

    TextField[] integerInputs = new TextField[] {
        timeSigUpper, timeSigLower, octaveLower, octaveUpper, patternLength, patternAmount
    };
    for (TextField integerInput : integerInputs ) {
        integerInput.setTextFormatter(new TextFormatter<>(integerInputValidator));
    }

    TextField[] floatInputs = new TextField[] { tempoInput, firstNoteLength, secondNoteLength };
    for (TextField floatInput : floatInputs ) {
        floatInput.setTextFormatter(new TextFormatter<>(floatInputValidator));
    }
    scaleInput.setTextFormatter(new TextFormatter<>(noteInputValidator));
    tempoSlider.valueProperty().addListener(new ChangeListener<Number>() {
          @Override
          public void changed( ObservableValue<? extends Number> observableValue,
                               Number oldValue, Number newValue) {
              tempoInput.setText(doubleToFixedPrecisionString(newValue.doubleValue()));
          }
    });

    attachTooltip(tempoInput, "The song tempo in BPM");
    attachTooltip(timeSigUpper, "The upper numeral of a time signature (e.g. the \"3\" in \"3/4\")");
    attachTooltip(timeSigLower, "The lower numeral of a time signature (e.g. the \"4\" in \"3/4\")");
    attachTooltip(firstNoteLength, "The duration (in quarter notes) of the first note");
    attachTooltip(secondNoteLength, "The duration (in quarter notes) of the second note");
    attachTooltip(scaleInput,
        "Comma separated list of all notes to use for the patterns. This can be\n" +
        "changed to any sequence (or length!) of notes you like, meaning you can\n" +
        "use exotic scales, or dictate the movement by creating a sequence in thirds,\n" +
        "re-introduce a previously defined note, etc. etc.\n" +
        "The scale will be repeated over the determined octave range. You can create sharps\n" +
        "and flats too, e.g.: \"Eb\", \"F#\", etc. are all valid input."
    );
    attachTooltip(octaveLower, "The lowest octave in the note range");
    attachTooltip(octaveUpper, "The highest octave in the note range");
    attachTooltip(patternLength, "The length of each generated pattern (in measures)");
    attachTooltip(patternAmount, "The amount of patterns to generate");
    attachTooltip(trackPerPattern,
        "Whether to separate each pattern into a unique MIDI track.\n" +
        "When the amount of patterns is high enough for the algorithm\n" +
        "to go back down the scale, it is best to have this set active\n" +
        "to avoid conflicts in MIDI notes."
    );
}