0

Playing around with my first JavaFX application. Running it on Java8 but that shouldn't be an issue regarding this question.

My problem: I have a scene (FXML) in which a menu and menu items exist. When one presses a menu item a new window or popup should show. This works just fine, but I want to disable the parent window while the new window is active. Figured out this is possible with modality. My real problem is: Determining the parent window from the action event I receive. Because the event comes from an menu item it seems a bit problematic. Probably a really stupid question.

My code snippet:

Stage stage = new Stage();
Parent root = FXMLLoader.load(EbooksdownloaderController.class.getResource("about.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node)event.getSource()).getScene().getWindow());
stage.show();

Casting the source to a Node gives a class casting exception. But I don't have a clue which other path to follow.

Thanks.

Pattux
  • 125
  • 1
  • 5
  • What is the exception you get? Have you tried debugging how could you access the Node from the event source? – rlegendi Jan 29 '14 at 08:01
  • It's a Class Cast Exception stating that a MenuItem cannot be cast to a Node. – Pattux Jan 29 '14 at 08:29
  • Take a look on this thread: http://stackoverflow.com/questions/10486731/how-to-create-a-modal-window-in-javafx-2-1 – rlegendi Jan 29 '14 at 09:54
  • Yep. From that thread I got my current non-working solution. The problem has todo something with the menu that behaves different than e.g. an ordinary button. – Pattux Jan 29 '14 at 10:35

1 Answers1

0

Have been fiddling around a while without any success. As a final resort I finished it using the following code:

@FXML
private AnchorPane ap;

Stage stage = new Stage();
Parent root = FXMLLoader.load(EbooksdownloaderController.class.getResource("about.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(ap.getScene().getWindow());
stage.show();

Not really the way I would prefer. But it works.

Pattux
  • 125
  • 1
  • 5