java JavaFX 类控制器舞台/窗口参考
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13015537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX Class controller Stage/Window reference
提问by nailujed
Is there any way of getting the Stage/Window object of an FXML loaded file from the associated class controller?
有没有办法从关联的类控制器获取 FXML 加载文件的 Stage/Window 对象?
Particularly, I have a controller for a modal window and I need the Stage to close it.
特别是,我有一个用于模态窗口的控制器,我需要舞台来关闭它。
回答by nailujed
I could not find an elegant solution to the problem. But I found these two alternatives:
我找不到这个问题的优雅解决方案。但我找到了这两种选择:
Getting the window reference from a Node in the Scene
@FXML private Button closeButton ; public void handleCloseButton() { Scene scene = closeButton.getScene(); if (scene != null) { Window window = scene.getWindow(); if (window != null) { window.hide(); } } }
Passing the Window as an argument to the controller when the FXML is loaded.
String resource = "/modalWindow.fxml"; URL location = getClass().getResource(resource); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(location); fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); Parent root = (Parent) fxmlLoader.load(); controller = (FormController) fxmlLoader.getController(); dialogStage = new Stage(); controller.setStage(dialogStage); ...
And FormController must implement the setStage method.
从场景中的节点获取窗口引用
@FXML private Button closeButton ; public void handleCloseButton() { Scene scene = closeButton.getScene(); if (scene != null) { Window window = scene.getWindow(); if (window != null) { window.hide(); } } }
在加载 FXML 时将 Window 作为参数传递给控制器。
String resource = "/modalWindow.fxml"; URL location = getClass().getResource(resource); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(location); fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); Parent root = (Parent) fxmlLoader.load(); controller = (FormController) fxmlLoader.getController(); dialogStage = new Stage(); controller.setStage(dialogStage); ...
并且 FormController 必须实现 setStage 方法。
回答by Jinu P C
@FXML
private Button closeBtn;
Stage currentStage = (Stage)closeBtn.getScene().getWindow();
currentStage.close();
Another way is define a static getter for the Stage and Access it
另一种方法是为舞台定义一个静态 getter 并访问它
Main Class
主班
public class Main extends Application {
private static Stage primaryStage; // **Declare static Stage**
private void setPrimaryStage(Stage stage) {
Main.primaryStage = stage;
}
static public Stage getPrimaryStage() {
return Main.primaryStage;
}
@Override
public void start(Stage primaryStage) throws Exception{
setPrimaryStage(primaryStage); // **Set the Stage**
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
}
Now you Can access this stage by calling
现在您可以通过调用访问此阶段
Main.getPrimaryStage()
Main.getPrimaryStage()
In Controller Class
在控制器类中
public class Controller {
public void onMouseClickAction(ActionEvent e) {
Stage s = Main.getPrimaryStage();
s.close();
}
}