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();
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
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();
}
I have an FXML file which has a split pane and 2 rectangles in it. I have both rectangles anchored properly via the FXML but from the code I am generating new rectangles but I can't seem to get the constraints on them working, I set them to the same settings as the rectangles in the FXML(constraint wise) but nothing. I think the issue is the rectangles in the FXML are inside a Split Pane where as the ones generated from the Java code are in the Main AnchorPane. Here is the code any ideas?
public void start(Stage stage) throws Exception {
//GridPane root = new GridPane();
AnchorPane root = fxmlLoader.load(getClass().getClassLoader().getResource("fxml/TestConveyorView.fxml")).getRoot();
Box box = new Box(1);
Rectangle rect = new Rectangle(50,50, box.getStatus().getColor());
rect.setX(385.0);
AnchorPane.setLeftAnchor(rect, 385.0);
AnchorPane.setRightAnchor(rect, 294.0);
root.getChildren().addAll(rect);
Scene scene = new Scene(root);
// BackgroundImage background = new BackgroundImage(null, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
stage.setScene(scene);
stage.show();
}
Fixed by removing all the extra code in the main class like below.
public void start(Stage stage) throws Exception {
AnchorPane root = fxmlLoader.load(getClass().getClassLoader().getResource("fxml/TestConveyorView.fxml")).getRoot();
Scene scene = new Scene(root);
// BackgroundImage background = new BackgroundImage(null, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
stage.setScene(scene);
stage.show();
}
And adding these to things to my controller
#FXML
AnchorPane Splitright;
Splitright.getChildren().add(rectangle);
As it turns out the shapes were just being thrown on top of everything not actually being placed in the proper AnchorPane, which is what I suspected was happening.
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.
I am using JavaFX 2 in Netbeans. How do I get the width and height of an element to adjust when the Frame is resized? Here is my layout:
Stage stage = new Stage();
stage.setTitle("Hello World");
final Group root = new Group();
Scene scene = new Scene(root);
BorderPane border = new BorderPane();
border.setPrefWidth(stage.getWidth());
border.setPrefHeight(stage.getHeight());
HBox outerHBox = new HBox();
border.setCenter(outerHBox);
root.getChildren().add(border);
stage.setScene(scene);
After some more research
I found where this has been done before http://java.dzone.com/articles/setting-stage-javafx-sdk but it is in an older FX (very different from JavaFX 2). I am having trouble translating it. Looks like I should be using binding? I've never used binding before and I've barely used FX.
Whats the best way to accomplish this?
Not all Node classes enable resizing. The Group class is one of these. You'll understand this when calling isResizable() on your root object. Use instead a subclass of Region e.g. BorderPane as your root.
Stage stage = new Stage();
stage.setTitle("Hello World");
final BorderPane border = new BorderPane();
Scene scene = new Scene(border);
Button button = new Button("test");
HBox outerHBox = new HBox();
outerHBox.getChildren().add(button);
outerHBox.setAlignment(Pos.CENTER);
border.setCenter(outerHBox);
stage.setScene(scene);
Your example should work, now.