我有一个Spinner控制器:
@FXML
private Spinner<Integer> spnMySpinner;
Run Code Online (Sandbox Code Playgroud)
和一个SimpleIntegerProperty控制器:
private static final SimpleIntegerProperty myValue =
new SimpleIntegerProperty(3); //load a default value
Run Code Online (Sandbox Code Playgroud)
我已在控制器的方法中将它们绑定在一起initialize:
spnMySpinner.getValueFactory().valueProperty().bindBidirectional(myValueProperty().asObject());
Run Code Online (Sandbox Code Playgroud)
但只有在控制器第二次初始化后,绑定才能正常工作。我可以通过以下方式重现它:
myValue正确指定的默认值(数字 3)。myValue保持不变,仍为数字 3。整个简约但可启动/可复制的代码:
主要.java:
package spinnerpoc;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new …Run Code Online (Sandbox Code Playgroud)