I am making a JavaFX app. I have create a Menuitem About. Upon clicking on the About Menuitem it will display a new window with some info about my app. The window is a Anchor Pane
with custom close button. I have set the stage undercoated at run time. I want to close this window without closing my main application. I don't want to set its visibility turn off on method call. I see some solution in net like Window existingWindow = ((Node) event.getSource()).getScene().getWindow();
but i can't use this as am getting error similar to this Menu item not cast node javafx scene. How can I achieve this goal?
Asked
Active
Viewed 1,444 times
0

Galib Mirza
- 17
- 5
-
Isn't there another node you can get the window from? – James_D Jan 14 '18 at 18:37
-
No I want it from about menuitem otherwise I have to set this functionality on button click. – Galib Mirza Jan 14 '18 at 18:43
-
Not sure I understand. Surely there are other nodes in the same window? Can you put some code in the question to show what you are doing? – James_D Jan 14 '18 at 18:43
-
I can't got your point. sorry. I am getting **about window** on clicking menuitem **about**. In about window there are four Label and one button. I wanna close it without closing my main application. – Galib Mirza Jan 14 '18 at 18:48
-
OK, so all you have to do is call `aboutWindow.hide()` where `aboutWindow` is a reference to the window are trying to close. You can get that reference with `label.getScene().getWindow()` or `button.getScene().getWindow()` where `label` and `button` are the nodes displayed in the window. What is the problem? – James_D Jan 14 '18 at 18:49
-
I also don't understand "otherwise I have to set this functionality on button click". Surely that is exactly where you want this? Aren't you wanting to close the window when the button is clicked? – James_D Jan 14 '18 at 18:51
-
yeah got it thanks. I am new. – Galib Mirza Jan 14 '18 at 18:55
-
Suppose if there is no other node there , is it possible to hide the window. Like if we click anywhere in the main window. – Galib Mirza Jan 14 '18 at 19:16
-
The root of the scene is a node. A stage must have a scene and a scene must have a root. You cannot have a window with nothing in it. Is there some reason you keep refusing to put any code in your question, or are you deliberately just trying to make it difficult to answer? – James_D Jan 14 '18 at 19:16
-
No. you help me a lot. I notice it in some app may be. Thanks again :) – Galib Mirza Jan 14 '18 at 19:31
-
Possible duplicate of [Unable to get Scene from MenuItem in JavaFX](https://stackoverflow.com/questions/20594392/unable-to-get-scene-from-menuitem-in-javafx) – fabian Jan 14 '18 at 20:30
1 Answers
0
Actually this is not my own answer, but I managed to understand @James_D. I usually create to manage open windows, otherwise I have to write a lot of code And this is solution code. In your controller class:
@FXML
void openAnotherWindow(ActionEvent actionEvent) {
try {
OpenWindow.openWindowMenuItem(someLabel, "views/some.fxml", "Title", 600, 400,
false, "resources/pictures/some_icon.png");
} catch (IOException e) {
e.printStackTrace();
}
}
And OpenWindow class
public class OpenWindow {
public static void openWindowMenuItem(Node label, String recource, String title,
int width, int height, boolean resizeable, String icon) throws IOException {
Parent root = FXMLLoader.load(Objects.requireNonNull(OpenWindow.class.getClassLoader().getResource(recource)));
Stage stage = new Stage();
Scene scene = new Scene(root, width, height);
if (icon != null) {
stage.getIcons().add(new Image(icon));
}
stage.setTitle(title);
stage.setResizable(resizeable);
stage.setScene(scene);
stage.show();
// close current window
label.getScene().getWindow().hide(); // this is key point
}
}
You can do it event without extra class. Hope it will help and again thanks to @James_D

Qudratxo'ja Musayev
- 486
- 4
- 11