How to create custom dialog with FXML in JavaFX?
In samples over the Net I see mostly something like this
#Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(
getClass().getResource( getClass().getSimpleName() + ".fxml" ));
Scene scene = new Scene(root);
i.e. FXML is loaded from within application start() and builds root node.
But what if I extend Stage? Where to load from FXML? In constructor? Or in initStyle()? Or in some other method?
You may use the below code in your main Class :
FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = (Parent)loader.load();
//Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
SampleController controller = (SampleController)loader.getController();
controller.setStageAndSetupListeners(stage);
After this in SampleController Make a function setStageAndSetupListeners(), which will accept your stage and now you use it easily.
Related
This question already has an answer here:
JavaFX loading a new fxml file into the same scene
(1 answer)
Closed last year.
I want to reload my Scene of the Java Fx project.
So i created a button, which has an Fx-ID
now i want to create something, that reloads the whole fx scene after pressing the button...
How is this possible.?
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("!");
stage.setScene(scene);
stage
this is my scene
Try
Stage close = (Stage) restart.getScene().getWindow();
close.close();
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("!");
stage.setScene(scene);
stage.show();
}
this should help to close your stage. you will also create another stage, with deleted inputs
I don't know how to close the main window in java fxml.
This part of code is in the class Main:
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent root2 = FXMLLoader.load(getClass().getResource("2ndwin.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
Scene scene2 = new Scene(root2);
secondaryStage.setScene(scene2);
}
public void show(){
secondaryStage.show();
}
I've got this. In controller i did this:
Main m = new Main();
m.show();`
but I still don't know how I can close primaryStage.
Please help me or tell me how I can create a new window and close the old window. I think this what I want to do - it isn't correct but I came up with it myself.
I do it by using an object in the stage you want to close to get the window
Window currentStage = OBJECTINSCENE.getScene().getWindow();
(Replace 'OBJECTINSCENE' with the id of anything in your scene). This gives you a reference to the stage you have open. Then call
currentStage.hide();
To close the stage when you want to.
So your show function would be as follows
public void show(){
Window currentStage = OBJECTINSCENE.getScene().getWindow();
secondaryStage.show();
currentStage.hide();
}
I have the following class which has a button.
public class GUI extends Application {
private BorderPane mainLayout = new BorderPane();
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Main Menu");
FlowPane layout = new FlowPane();
Button button = new Button("Click");
layout.getChildren().addAll(button);
mainLayout.setTop(layout);
Scene scene = new Scene(mainLayout, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
If I have another class with a scene, how can I update the GUI class to show the scene by pressing the button?
The preferred mechanism would probably be to get the stage dynamically from a trigger event, for example:
button.setOnAction(event -> {
Scene newScene = // ... commands which define the new scene.
Stage stage = ((Node) event.getTarget()).getScene().getStage();
// or alternatively, just:
// Stage stage = button.getScene().getStage();
stage.setScene(newScene);
});
An alternative is to provide a static accessor to the main stage in the Application.
Change your GUI class to add an accessor for the stage:
public class GUI extends Application {
private static Stage guiStage;
public static Stage getStage() {
return guiStage;
}
#Override
public void start(Stage primaryStage) {
guiStage = primaryStage;
// other app initialization logic . . .
}
}
In your class which needs to change the scene for the GUI stage to a new scene, invoke:
Scene newScene = // ... commands which define the new scene.
GUI.getStage().setScene(newScene);
Using a static accessor in this specific instance is generally OK, because you can only have a single Application instance launched for a given JVM execution. The only real drawback is that you have a coded dependency between the class creating your new scene and your Application class. But, for some application types, this won't matter.
In a javafx application, I have my stage which contains an unique StackPanel.
In this StackPanel, I add the Panel I want to display (depending on what the user want).
Loading the StackPanel view :
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
stackLayout = (StackPane) loader.load();
Scene scene = new Scene(stackLayout);
stage.setScene(scene);
stage.show();
Adding a view to this StackPanel :
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MapGame.fxml"));
AnchorPane map = (AnchorPane) loader.load();
stackLayout.getChildren().clear();
stackLayout.getChildren().add(map);
What I want is to automatically resize my StackPane and Stage depending on the size of the child of my StackPane...
For example, my StackPane is 600x400, and the AnchorPane is 800x600, but when the Application is shown, the size is 600x400... Anybody ?
After these lines
stage.setScene(scene);
stage.show();
add the following:
stage.sizeToScene();
Edit: Try using this order in your code:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
stackLayout = (StackPane) loader.load();
loader.setLocation(Main.class.getResource("view/MapGame.fxml"));
AnchorPane map = (AnchorPane) loader.load();
stackLayout.getChildren().clear();
stackLayout.getChildren().add(map);
Scene scene = new Scene(stackLayout);
stage.setScene(scene);
stage.show();
stage.sizeToScene();
My app, if I click the "maximize button", it will maximize the window. But if I go to another scene(in the same stage), the window will restore to the original size. So, how can I control it to keep maximizing?
I used primaryStage.setMaximized(true); after calling show(); method in Java 8 implementation. It keeps other scenes maximizing.
I had the same problem. I fixed it creating a root stage with that only contains a menu bar and a tool bar, like this:
I initialized this window in the start method with:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
RootController controller= loader.getController();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
Later, when you want to load a FXML file or scene into the root container, you could make it with the next lines in another method:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/principal.fxml"));
AnchorPane principalOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(principalOverview);
// Get the controller instance
controllerPrincipal = loader.getController();
in that way every scene you add to the root stage will get the size of the root.
This is how I did it:
#FXML
private Button goBtn;
Stage stage = (Stage) goBtn.getScene().getWindow();
Scene scene = goBtn.getScene();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Activity.fxml"));
scene.setRoot(loader.load());
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}