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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…