英文:
Javafx Initialize Controller Constructor only Once?
问题
我正在构建一个 JavaFX GUI 应用程序,我遇到了这个问题。我为我的程序有多个场景,因此我有不同的 FXML 文件和控制器类。问题是在第一个“主菜单”(在我的应用程序中首先弹出的场景)场景中,在控制器的构造函数中,我调用一些从数据库加载数据等重型方法。所以问题是这样的。在接下来的场景中,我有“主菜单按钮”,可以切换到“主菜单”场景!所以每次我返回到“主菜单”场景时,构造函数都会调用重型加载数据的方法。而我不希望这样。我不想每次都调用这些方法,只想在开始时调用一次。
以下是一些简化的示例代码:
主菜单场景(控制器)
public class MainController {
@FXML
Button bt1 = new Button();
@FXML
Button bt2 = new Button();
@FXML
Button bt3 = new Button();
public static int choice=0;
//构造函数
public MainController(){
try {
// 在这里执行繁重的数据库任务(加载数据)
}catch (Exception e){
//错误处理
}
}
@FXML
public void initialize(){}
}
另一个控制器类
public class Scene2Controller {
private Button mainMenu = new Button();
//构造函数
public Scene2Controller(){}
@FXML
public void initialize(){}
public void goMainMenu(ActionEvent actionEvent) throws IOException {
Parent menu= FXMLLoader.load(getClass().getResource("/mainScene.fxml"));
Stage window = (Stage) mainMenu.getScene().getWindow();
window.getScene().setRoot(menu);
window.show();
}
}
所以在第二个控制器中,我有一个侦听方法,当按钮被点击时,它会返回到主菜单场景,加载适当的 FXML 文件。
我理解对于大多数人来说,这似乎非常简单,但我是 JavaFX 的新手,我想知道在切换场景方面是否有什么做得不对的地方,或者是否应该采取不同的方法,以便我在主菜单构造器类中拥有的这些方法只运行一次。这是否可行?还是我应该创建一个在主菜单场景之前运行的子控制器类?提前谢谢。
英文:
I'm building a Javafx gui application and I'm facing this problem. I do have multiple Scenes for my program and so i have different FXML files and Controller classes. The thing is that in the first "Main Menu" (which is the first that pops up in my app) scene, in the Constructor of the Controller i call some heavy methods loading data from database and more.
So what happens it this. In the next scenes i do have "Main Menu Buttons", that switch to Main Menu scene! So every time i go back to the "Main Menu" scene the constructor call the heavy methods loading data. Whereas i don't want that. I don't want to call these methods every time, just Once at the start.
Here is some example code simplified :
Main Menu Scene(Controller)
public class MainController {
@FXML
Button bt1 = new Button();
@FXML
Button bt2 = new Button();
@FXML
Button bt3 = new Button();
public static int choice=0;
//constructor
public MainController(){
try {
//heavy databse tasks here(loading data)
}catch (Exception e){
//error handling
}
}
@FXML
public void initialize(){}
}
Another Controller class
public class Scene2Controller {
private Button mainMenu = new Button();
//constructor
public Scene2Controller(){}
@FXML
public void initialize(){}
public void goMainMenu(ActionEvent actionEvent) throws IOException {
Parent menu= FXMLLoader.load(getClass().getResource("/mainScene.fxml"));
Stage window = (Stage) mainMenu.getScene().getWindow();
window.getScene().setRoot(menu);
window.show();
}
}
So in the Second controller i have a listener method that when buttons clicks it goes back to the main menu scene, loading the appropriate FXML file.
I understand that this seams pretty straight forward to most of you, but im new in javafx and i wanted to know if i there is something doing wrong switching scenes or that i should do different in order for these methods that i have in the mainMenu Constructor class, to run Only Once. Is that obtainable or should i create a Sub-Controller class that run before the Main Menu Scene? Thanks in advance.
答案1
得分: 0
在我的情况下,我不得不调用几个方法,所以我只是从第一个场景控制器类中将它们移除,并且将它们添加到我的MainClass
的start()
方法中,而不是放在一个控制器类内部。我认为@James_D的回答对于类似我这样的问题是最好的。谢谢!
英文:
In my case i had to call few methods, so i just removed them from the first Scene Controller class and add them in the start()
method in my MainClass
and not inside a Controller class. I think @James_D 's response is the overall best for a similar question like mine one. Thanks !
专注分享java语言的经验与见解,让所有开发者获益!
评论