JavaFx: How to keep the window maximizing? - java

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();
}

Related

Reload Java Fx Scene? [duplicate]

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

stack panel and scene auto-resize

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();

Can't create new stage with multiple AnchorPanes

I'm working on this simple application and trying to create a new stage/window when clicking on a button. I get this work when using a simple fxml-document with only one AnchorPane. In the scene I want to pop-up, there is a TabPane with two tabs. I've imported one fxml-document into each tab, and when I do that a new AnchorPane is created in each tab. So when I click the button in main scene nothing is happening. I really don't know how to solve this, does anybody have any ideas?
This is the code I'm using right now to show a new stage.
#FXML
private void showAddStage(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Add.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
} catch (IOException e) {}
}

How to create custom dialog with FXML in JavaFX?

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.

is it possible to open a new fxml file in the same frame

i've created fxml files in anchor pane
but everytime i click on the button i get the next fxml in a new frame
i want it to open in the same frame
public void baropen(ActionEvent event) {
// handle the event here
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(10, 50, 50, 50));
Stage stage = new Stage();
Scene scene ;
// scene= new Scene(root);
scene = new Scene(bp);
stage.setScene(scene);
stage.show();
try {
new RecBar().start(stage);
} catch (Exception ex) {
Logger.getLogger(RecController.class.getName()).log(Level.SEVERE, null,ex);
}
}
Just create a single Stage and when you want to replace the content of the stage with a new fxml, load the new fxml into a new Scene and call stage.setScene.
It's a theater metaphor - imagine you are watching a play - it's Romeo and Juliet, the curtain raises and you see the first scene (a plaza in Verona with a fountain). Later on, the curtain lowers, little men run around and change stuff, the curtain rises and you see a new scene (the balcony to Juliet's bedroom). The scene has changed, but the stage has not - there is just one stage and multiple scenes.

Categories