TextField textField = new TextField(); textField.textProperty().addListener((observable, oldValue, newValue) -> { System.out.println("textfield changed from " + oldValue + " to " + newValue); });
TextField txtMinPrice, txtMaxPrice = new TextField(); ChangeListener<String> forceNumberListener = (observable, oldValue, newValue) -> { if (!newValue.matches("\\d*")) ((StringProperty) observable).set(oldValue); }; txtMinPrice.textProperty().addListener(forceNumberListener); txtMaxPrice.textProperty().addListener(forceNumberListener);
// t is an instance of Template final Map<String,String> params = t.getParameters(); Iterator<Map.Entry<String,String>> it = params.entrySet().iterator(); Map.Entry<String,String> param; int i; for (i=0, param = it.hasNext() ? it.next() : null ; param != null ; i++, param = it.hasNext() ? it.next() : null) { Label label = new Label(); TextField field = new TextField(param.getValue()); final String key = param.getKey(); field.textProperty().addListener((ObservableValue<? extends String> obs, String oldValue, String newValue) -> params.put(key,newValue)); // unrelevant code }
for (int i = 0; i < 10; i++) { TextField tf = new TextField(); final int index = i; tf.textProperty().addListener((obs, oldVal, newVal) -> { System.out.println("Text of Textfield on index " + index + " changed from " + oldVal + " to " + newVal); }); lfLetters.add(tf); }
TextField field = new TextField(); field.textProperty().addListener(new ChangeListener<String>(){ @Override public void changed(ObservableValue<? extends String> value, String oldValue, String newValue) { //Do your validation or revert the value }});
public class StringParameter extends Parameter<String> { private final TextField editor ; public StringParameter(String name, String value) { super(name, value); editor = new TextField(); editor.textProperty().bindBidirectional(valueProperty()); } @Override public Node getEditor() { return editor ; } }
TextField txt = new TextField(); txt.setPromptText("Search"); ChangeListener<String> txtListener = (observable, oldValue, newValue) -> System.out.println( "Search text updated to: " + newValue ); txt.textProperty().addListener( txtListener ); txtListener.changed( txt.textProperty(), null, "frobozz" );
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty()); firstNameCol.setCellFactory(col -> { TableCell<Person, String> cell = new TableCell<>(); TextField textField = new TextField(); cell.graphicProperty().bind(Bindings.when(cell.emptyProperty()) .then((Node)null) .otherwise(textField)); textField.textProperty().bindBidirectional( EasyBind.monadic(cell.tableRowProperty()) .selectProperty(TableRow::itemProperty) .selectProperty(p -> ((Person)p).firstNameProperty())); return cell ; });
GridPane p = new GridPane(); TextField tf = new TextField("DEFAULT"); Label l1 = new Label("..."); l1.textProperty().bind(tf.textProperty()); p.add(tf, 0, 0); p.add(l1, 1, 0); Scene sc = new Scene(p, 500, 500); arg0.setScene(sc); arg0.show();
TextField fileInput = new TextField(); Button inputButton = new Button("Upload File"); inputButton.disableProperty().bind(Bindings.createBooleanBinding( () -> ! new File(fileInput.getText()).exists(), fileInput.textProperty())); inputButton.setOnAction(e -> { inputFile = new File(fileInput.getText()); window.close(); });
TextField fileInput = new TextField(); Button inputButton = new Button("Upload File"); inputButton.setDisable(true); fileInput.textProperty().addListener((obs, oldText, newText) -> inputButton.setDisable( ! new File(newText).exists() ) ); inputButton.setOnAction(e -> { inputFile = new File(fileInput.getText()); window.close(); });
TextField textField = new TextField(); textField.setPromptText("Enter text"); PseudoClass empty = PseudoClass.getPseudoClass("empty"); textField.pseudoClassStateChanged(empty, true); textField.textProperty().addListener((obs, oldText, newText) -> { textField.pseudoClassStateChanged(empty, newText.isEmpty()); });
public void start(Stage primaryStage) { Pane root = new Pane(); TextField enterText = new TextField(); enterText.setFont(Font.font("SanSerif",20)); text.bind(enterText.textProperty()); text.addListener((obs, oldTextValue, newTextValue) -> System.out.println("The new value is "+newTextValue)); root.getChildren().addAll(enterText); Scene scene = new Scene(root,600,600); primaryStage.setScene(scene); primaryStage.setTitle("Text Entry"); primaryStage.show(); }
@Override public void start( final Stage primaryStage ) { TextField textfield = new TextField(); Button button = new Button( "my button" ); button.visibleProperty().bind( textfield.textProperty().isEmpty().not() ); final Scene scene = new Scene( new HBox( button, textfield ), 800, 600 ); primaryStage.setScene( scene ); primaryStage.show(); }
final TextField textField1 = new TextField(); final TextField textField2 = new TextField(); final TextField textField3 = new TextField(); BooleanBinding bb = new BooleanBinding() { { super.bind(textField1.textProperty(), textField2.textProperty(), textField3.textProperty()); } @Override protected boolean computeValue() { return (textField1.getText().isEmpty() && textField2.getText().isEmpty() && textField3.getText().isEmpty()); } }; Button btn = new Button("Button"); btn.disableProperty().bind(bb); VBox vBox = new VBox(); vBox.getChildren().addAll(textField1, textField2, textField3, btn);
TextField textField = new TextField(); textField.setPromptText("Enter something"); PseudoClass empty = PseudoClass.getPseudoClass("empty"); textField.pseudoClassStateChanged(empty, textField.getText().isEmpty()); textField.textProperty().isEmpty().addListener((obs, wasEmpty, isNowEmpty) -> textField.pseudoClassStateChanged(empty, isNowEmpty));
final List<Row> rows = new ArrayList<>(); addTableField.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { HBox box = new HBox(10); ComboBox<String> combo = new ComboBox<String>(fieldTypes); combo.setPromptText("type"); TextField field = new TextField(); field.setPromptText("field label"); box.getChildren().addAll(field, combo); fieldContainer.getChildren().addAll(box); Row row = new Row(); rows.add(row); row.labelProperty().bind(field.textProperty()); row.typeProperty().bind(combo.valueProperty()); // might need to worry about null values... window.sizeToScene(); } });
public class MyApp extends Application { private static final String APP_NAME = "My App "; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { TextField titleTextField = new TextField(); GridPane grid = new GridPane(); grid.add(titleTextField, 1, 1); Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene); primaryStage.titleProperty().bind(Bindings.concat(APP_NAME).concat(titleTextField.textProperty())); primaryStage.show(); } }
TextField tf = new TextField(); final PseudoClass shortText = PseudoClass.getPseudoClass("short"); final PseudoClass longText = PseudoClass.getPseudoClass("long"); tf.textProperty().addListener((observable, oldValue, newValue) -> { tf.pseudoClassStateChanged(shortText, false); tf.pseudoClassStateChanged(longText, false); if (newValue!=null && !newValue.isEmpty()) { if (newValue.length() < 5) { tf.pseudoClassStateChanged(shortText, true); } else { tf.pseudoClassStateChanged(longText, true); } } });
TextField tf = new TextField(); // Set Max and Min Width to PREF_SIZE so that the TextField is always PREF tf.setMinWidth(Region.USE_PREF_SIZE); tf.setMaxWidth(Region.USE_PREF_SIZE); tf.textProperty().addListener((ov, prevText, currText) -> { // Do this in a Platform.runLater because of Textfield has no padding at first time and so on Platform.runLater(() -> { Text text = new Text(currText); text.setFont(tf.getFont()); // Set the same font, so the size is the same double width = text.getLayoutBounds().getWidth() // This big is the Text in the TextField + tf.getPadding().getLeft() + tf.getPadding().getRight() // Add the padding of the TextField + 2d; // Add some spacing tf.setPrefWidth(width); // Set the width tf.positionCaret(tf.getCaretPosition()); // If you remove this line, it flashes a little bit }); }); tf.setText("Hello World!");