JavaFX从另一个控制器更新控制器属性 [英] JavaFX updating controller property from another controller
问题描述
我有一个进度条,正在尝试在控制器的javaFX中更新。我正在基于一个称为Generate()的函数更新进度条,如果调用它应该更新主控制器中的进度条。但是,我拥有的代码不会更新它,而是会更新进度条的新实例。
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.
我的DrawerContentController中的Generate方法是:
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();
}
在我的主控制器中,我使用场景生成器通过fxml设置了progressBar,我正在尝试从DrawerContentController更新progressBar,这实际上是一个包含3个按钮的侧边栏菜单,其中一个是generate,它调用了Generate()方法。我知道我可能应该使用线程,我是JavaFX的初学者,并且仍在学习如何充分使用它。
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.
我也尝试过:
FXMLLoader fxmlLoader = new FXMLLoader((getClass().getResource("layout.fxml")));
然后声明控制器并通过
FXMLDocumentController fxmldc = fxmlLoader.getController();
然后评估房产,但是我以这种方式得到了回报。
and then assessing the property, however I get a npe this way.
我的FXMLDocumentController
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();
}
}
推荐答案
在 DrawerContentController
中创建一个可观察的属性以获取进度:
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...
}
现在,您可以将进度条的progress属性绑定到控制器的progress属性:
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
}
// ...
}
现在在 DrawerContentController
类中,如果您执行 this.setProgress(。 ..)
(更新您定义的新的进度属性),它将自动更新进度栏。
Now in your DrawerContentController
class, if you do this.setProgress(...)
(updating the new progress property you defined), it will automatically update the progress bar.
这篇关于JavaFX从另一个控制器更新控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!