如何从MenuItem On Action方法调用Stage.initOwner()?

时间:2014-05-25 03:52:29

标签: java user-interface javafx

我刚来自Is There any Way to Create A Child Window That Has the Same Properties as the Parent?,我试图致电stage.initOwner(((Node) event.getSource()).getScene().getWindow());,但它没有成功,我得到了一个例外。我不认为我正确地使用了这个。我应该如何尝试从onAction方法调用它?

原始代码块:

@FXML
private void onNewClanCreation(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("ClanCreationPanel.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.setResizable(false);
stage.setTitle("New Clan Creation");
stage.show();
}

例外:

`Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node at com.gmail.physicistsarah.program.core.ControlPanelController.onNewClanCreation(ControlPanelController.java:65)
... 54 more`

编辑:添加了stage.initOwner

1 个答案:

答案 0 :(得分:2)

这有效: stage.initOwner(this.nameLabel.getScene().getWindow());谢谢@ItachiUchiha