Reputation: 111
In my application I want the user user to be able to click on a menu item and have a new window appear. However, when I run my code it says that you can open a new window from a menu item. So my question is,is there another work around or another simpler way for me to accomplish my goal. Thanks for your help.
P.S --> I had posted a similar question earlier, but had made a mistake in copying the correct code I had. This is the correct error and code I get.
Code:
/**
* When the Logger menu item is clicked, then it will execute and make a new window
* @param actionEvent
* @throws Exception
*/
public void clickedLoggerMenu(ActionEvent actionEvent) throws Exception {
//The name of the controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/viewer_logger.fxml"));
Parent home_page = (Parent) loader.load();
LoggerController logController = loader.getController();
//this sets the scene
Scene home_page_scene = new Scene(home_page, 650, 580);
Stage app_stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
app_stage.setScene(home_page_scene);
app_stage.show();
}
Exception:
Caused by: java.lang.ClassCastException: [email protected]/javafx.scene.control.MenuItem cannot be cast to [email protected]/javafx.scene.Node
at controller.SubmitController.clickedLoggerMenu(SubmitController.java:99)
Upvotes: 1
Views: 43
Reputation: 10253
The problem is in how you are trying to create the Stage
:
Stage app_stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
You are trying to cast the MenuItem
to a Node
and that is not how it works.
You also need to create a new Stage
anyway, so do not try to get the original Window
.
Stage app_stage = new Stage()
Then build the Scene
from there.
Side Note: I recommend reading some good JavaFX tutorials to learn the basics.
Upvotes: 1