Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

java - JavaFX updating controller property from another controller

I have a progress bar that I am trying to update within javaFX in a controller. I am updating the progress bar based on a function called Generate(), if it is called it should update the progress bar within the main controller. However, the code I have doesn't update it, rather it updates a new instance of the progress bar.

The Generate method in my DrawerContentController is :

    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();
}

In my main controller I have the progressBar set up via fxml using scene builder, i'm trying to update the progressBar from DrawerContentController, which is essentially a sidebar menu that consists of 3 buttons, one of which is generate that calls the Generate() method. I know I should probably be using threads, I am a beginner to JavaFX and still learning on how to use it fully.

I've also tried :

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

and then declaring the controller and instantiating it by

FXMLDocumentController fxmldc = fxmlLoader.getController();

and then assessing the property, however I get a npe this way.

My 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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just create an observable property in DrawerContentController for the progress:

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...

}

Now you can bind your progress bar's progress property to the controller's progress property:

@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
    }

    // ...
}

Now in your DrawerContentController class, if you do this.setProgress(...) (updating the new progress property you defined), it will automatically update the progress bar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...