JavaFX从另一个控制器更新控制器属性

时间:2017-03-15 18:02:10

标签: java javafx controller scenebuilder

我有一个进度条,我试图在控制器中的javaFX中更新。我正在根据一个名为Generate()的函数更新进度条,如果它被调用它应该更新主控制器中的进度条。但是,我所拥有的代码并没有更新它,而是更新了进度条的新实例。

我的DrawerContentController中的Generate方法是:

    try {
    AnchorPane ap = fxmlLoader.load();
    for(Node node: ap.getChildren()){
        if(node.getId().equals("progressBar")){
            progressBar = (ProgressBar) node;
            progressBar.setProgress(50);
        }
    }

} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

在我的主控制器中,我使用场景构建器通过fxml设置progressBar,我试图从DrawerContentController更新progressBar,这实际上是一个由3个按钮组成的侧边栏菜单,其中一个是生成的调用Generate()方法。我知道我应该使用线程,我是JavaFX的初学者,并且仍在学习如何完全使用它。

我也尝试过:

FXMLLoader fxmlLoader = new FXMLLoader((getClass().getResource("layout.fxml")));

然后声明控制器并通过

实例化它
FXMLDocumentController fxmldc = fxmlLoader.getController();

然后评估财产,但是我这样得到了。

我的FXMLDocumentController

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    //Load Splash screen
    if (!MainClass.isSplashLoaded)
        loadSplashScreen();

    //load drawer content
    try {
        VBox box = FXMLLoader.load(getClass().getResource("drawerContent.fxml"));
        drawer.setSidePane(box);

        HamburgerBasicCloseTransition transition = new HamburgerBasicCloseTransition(hamburger);
        transition.setRate(-1);
        hamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
            transition.setRate(transition.getRate() * -1);
            transition.play();

            if (drawer.isShown()) {
                drawer.close();
                mainText.setVisible(true);
            } else {
                drawer.open();
                mainText.setVisible(false);
            }

        });
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

只需在DrawerContentController中为进度创建一个可观察的属性:

public class DrawerContentController implements Initializable {

    private final DoubleProperty progress = new SimpleDoubleProperty();

    public DoubleProperty progressProperty() {
        return progress ;
    }

    public final double getProgress() {
        return progressProperty().get();
    }

    public final void setProgress(double progress) {
        progressProperty().set(progress);
    }

    // existing code...

}

现在,您可以将进度条的progress属性绑定到控制器的progress属性:

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    //Load Splash screen
    if (!MainClass.isSplashLoaded)
        loadSplashScreen();

    //load drawer content
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("drawerContent.fxml"));
        VBox box = loader.load();

        DrawerContentController drawerContentController = loader.getController();
        progressBar.progressProperty().bind(drawerContentController.progressProperty());

        drawer.setSidePane(box);

        // ... existing code
    }

    // ...
}

现在在DrawerContentController课程中,如果你this.setProgress(...)(更新你定义的新进度属性),它会自动更新进度条。

相关问题