i'm new to Javafx and in generell GUI-programming. I'm working on an application which e.g. Labels need to be updated, as soons as there are any changes on the variable. I already use Properties and Bindings, but the change is only visible if i close an reopen the window. To be clearer, i don't mean change a Label if the User writes in a Textfiel, with that i have no problems. But with Labels that holds Integer or Strings and don't really have a trigger.
So my generell question is: How do i update something on the application, as soon as something changes. I recreated an simple example, so that maybe you guys can undertand what I want to ask (not a native speaker, sorry), but without the option to close an reopen the window.
Main:
public class Test extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Stage window = primaryStage;
window.setTitle("Configuration");
FlowPane numberOfDirPane = new FlowPane();
Label dirLabel = new Label("Number of Directories: ");
Label numberOfDirLabel = new Label("");
numberOfDirPane.getChildren().addAll(dirLabel, new Label (Integer.toString(DataModel.number.get())));
numberOfDirPane.setAlignment(Pos.CENTER);
FlowPane editDir = new FlowPane();
Label editLabel = new Label("Edit Number of Directories: ");
TextField numberDirText = new TextField();
numberDirText.setMaxWidth(50);
Button editDirNumberButton = new Button("Confirm");
editDirNumberButton.setOnAction(e -> {
DataModel.getNumber(Integer.parseInt(numberDirText.getText()));
numberOfDirLabel.textProperty().bind(DataModel.number.asString());
});
editDir.getChildren().addAll(editLabel, numberDirText, editDirNumberButton);
editDir.setAlignment(Pos.CENTER);
editDir.setHgap(40);
FlowPane buttons = new FlowPane();
Button confirmEditButton = new Button("Confirm");
Button closeButton = new Button("Close");
closeButton.setOnAction(e -> window.close());
buttons.getChildren().addAll(confirmEditButton, closeButton);
buttons.setAlignment(Pos.CENTER);
buttons.setHgap(20);
VBox layout = new VBox(20);
layout.getChildren().addAll(numberOfDirPane, editDir, buttons);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout, 500, 300);
window.setScene(scene);
window.show();
}
}
DataModel:
public class DataModel {
public static IntegerProperty number = new SimpleIntegerProperty(0);
public static void getNumber(int number) {
DataModel.number.set(number);
System.out.println("DataModel " + number);
}
}