Code: Select all
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class MainWindow extends Application {
StringConverter conv;
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
StringProperty test = new SimpleStringProperty("");
TextField tf1 = new TextField();
TextField tf2 = new TextField();
conv = new StringConverter() {
@Override
public String fromString(String str) {
if (str == null)
return "";
return str;
}
@Override
public String toString(String str) {
if (str == null)
return "";
return str;
}
};
tf1.textProperty().bindBidirectional(test, conv);
tf2.textProperty().bindBidirectional(test, conv);
Label currentValLabel = new Label(""+test.get());
test.addListener((obs, oldVal, newVal) -> {
currentValLabel.setText("Valor: "+ newVal);
});
borderPane.setTop(tf1);
borderPane.setBottom(tf2);
borderPane.setCenter(currentValLabel);
Scene scene = new Scene(borderPane, 600, 400);
primaryStage.setTitle("BorderPane en JavaFX");
primaryStage.setScene(scene);
primaryStage.show();
}