Looking at this code they show a way to display a new window after a login. When username and password are correct it opens new dialog. I want a button click to open new dialog, without checking for username and password.
Asked
Active
Viewed 2e+01k times
51
-
11It's important that you take the time to learn this kind of stuff. I you are looking to use JavaFX in any way, I would suggest going through a few tutorials. I would recommend [this one](http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm) – blo0p3r Feb 23 '13 at 15:07
-
1The link is broken – ds_practicioner Sep 13 '20 at 02:57
3 Answers
105
If you just want a button to open up a new window, then something like this works:
btnOpenNewWindow.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getClassLoader().getResource("path/to/other/view.fxml"), resources);
Stage stage = new Stage();
stage.setTitle("My New Stage Title");
stage.setScene(new Scene(root, 450, 450));
stage.show();
// Hide this current window (if this is what you want)
((Node)(event.getSource())).getScene().getWindow().hide();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

Timothy Cope
- 482
- 2
- 11

blo0p3r
- 6,790
- 8
- 49
- 68
-
3
-
1@will `close`ing and `hide`ing a window is the same thing. [this answer](http://stackoverflow.com/a/10217157/686036) is good at explaining this concept. – blo0p3r Oct 28 '14 at 12:06
-
I mean drop it as-in delete the instance. I have a long running app (weeks) and I don't need any resources un-garbage collected to gather lint. – will Oct 28 '14 at 12:14
-
4That's exactly it. `hide`ing it or `close`ing it will remove it. Just delete any reference to this window/stage (such you would do with any other java object). – blo0p3r Oct 28 '14 at 12:17
-
I'm trying to hide window from menuitem `ActionEvent` but getting exception "menuitem cannot be casted to Node". `Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node` – Inzimam Tariq IT Oct 07 '17 at 21:45
-
-
1@Line In this example not really. But assuming you want to do something else with `root` outside of the try block then it would be useful. – blo0p3r Jan 25 '18 at 13:20
21
I use the following method in my JavaFX applications.
newWindowButton.setOnMouseClicked((event) -> {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("NewWindow.fxml"));
/*
* if "fx:controller" is not set in fxml
* fxmlLoader.setController(NewWindowController);
*/
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
Stage stage = new Stage();
stage.setTitle("New Window");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
Logger logger = Logger.getLogger(getClass().getName());
logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
});

SkidRunner
- 813
- 8
- 12
4
The code below worked for me I used part of the code above inside the button class.
public Button signupB;
public void handleButtonClick (){
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("sceneNotAvailable.fxml"));
/*
* if "fx:controller" is not set in fxml
* fxmlLoader.setController(NewWindowController);
*/
Scene scene = new Scene(fxmlLoader.load(), 630, 400);
Stage stage = new Stage();
stage.setTitle("New Window");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
Logger logger = Logger.getLogger(getClass().getName());
logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
}
}

Leonardo
- 49
- 3