Loading new fxml in the same scene

2019-01-01 03:53发布

I have 2 fxml files:

  • Layout (header, menubars and content)
  • Anchorpane (it's supposed to be placed inside the content from the other fxml file)

I would like to know how can I load the second file inside the content space from the "Master" scene. And is that a good thing to do working in javaFX or is it better to load a new scene?

I'm trying to do something like this, but it doesn't work:

@FXML
private AnchorPane content;

@FXML
private void handleButtonAction(ActionEvent event) {        
    content = (AnchorPane) FXMLLoader.load("vista2.fxml");
}

Thanks for the help.

7条回答
听够珍惜
2楼-- · 2019-01-01 04:19

Why your code does not work

The loader creates a new AnchorPane, but you never add the new pane to a parent in the scene graph.

Quick Fix

Instead of:

content = (AnchorPane) FXMLLoader.load("vista2.fxml");

Write:

content.getChildren().setAll(FXMLLoader.load("vista2.fxml"));

Replacing the content children with your new vista. The content itself remains in the scene graph, so when you set it's children, you are also attaching them to the scene graph at the same time.

You might need to play around with layout (e.g. work with auto resizing layouts like StackPanes rather than AnchorPanes) to get the exact behaviour you want.

Rather than just adopting the quick fix, I would advise reviewing the simple framework linked below as that might provide you with a more general purpose mechanism to get the behaviour you want.

Reference FXML Navigation Framework

I created a small framework for swapping fxml controlled content panes in and out of a portion of the main scene.

The mechanism of the framework is the same as suggested in kithril's answer.

  1. A main pane for the outer fxml acts as a holder for child panes.
  2. The main controller for the outer fxml supplies a public method that can be used to swap the child panes.
  3. A convenience navigator class is statically initialized with the main controller for the outer layout.
  4. The navigator provides a public static method to load a new child pane into the main container (by invoking a method on the main controller).
  5. Child panes are generated in the navigator by their respective fxml loaders.

Why a Framework

The framework seems like overkill for answering your question, and perhaps it is. However, I have found that the two most asked topic related to FXML are:

  1. Navigation between panes generated by FXML (this question).
  2. How to pass data between FXML controllers.

So I felt that a small demo framework was warranted for this case.

Sample Framework Output

The first screen shows the application layout displaying the first vista. The contents are a header which is defined in the main application layout and an aliceblue colored interchangable child content pane.

vista1

In the next screen, the user has navigated to the second vista, which retains the constant header from the main layout and replaces the original child pane with a new coral colored child content pane. The new child has been loaded from a new fxml file.

vista2

Looking for Something More Substantial?

A lightweight framework which is more extensive and better supported than the sample framework from this question is afterburner.fx.

Looking for Something Even Simpler?

Just swap out the scene root: Changing Scenes in JavaFX.

Other Options?

Animated Transitions and others: Switch between panes in JavaFX

查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 04:23

Im not sure about how effective this is, but seems to be just fine and what's more, much simpler to methods above.

https://www.youtube.com/watch?v=LDVztNtJWOo

As far as I understood what is happening here is this(its really similiar to what is happening in Start() method in application class) :

private void buttonGoToWindow3Action(ActionEvent event) throws IOException{
    Parent window3; //we need to load the layout that we want to swap
    window3 = (StackPane)FXMLLoader.load(getClass().getResource("/ScenePackage/FXMLWindow3"));

    Scene newScene; //then we create a new scene with our new layout
    newScene = new Scene(window3);

    Stage mainWindow; //Here is the magic. We get the reference to main Stage.
    mainWindow = (Stage)  ((Node)event.getSource()).getScene().getWindow();

    mainWindow.setScene(newScene); //here we simply set the new scene
}

However Im not a java expert and quite new to programing so it would be good if someone experienced would evaluate it.

EDIT: Ive found even simpler method;

Go to MainApplication class and make static Stage parentWindow.

public static Stage parentWindow;
@Override
public void start(Stage stage) throws Exception {
    parentWindow = stage;

    Parent root = FXMLLoader.load(getClass().getResource("/ScenePackage/FXMLMainScene.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

Now you get acces to your main Stage so anywhere in a program you can do something like that to change the scene:

    Parent window1;
    window1 = FXMLLoader.load(getClass().getResource("/ScenePackage/FXMLWindow1.fxml"));

    //Scene newSceneWindow1 = new Scene(window1);

    Stage mainStage;
    //mainStage = (Stage)  ((Node)event.getSource()).getScene().getWindow();
    mainStage = MainApplication.parentWindow;
    mainStage.getScene().setRoot(newSceneWindow1); //we dont need to change whole sceene, only set new root.
查看更多
余生请多指教
4楼-- · 2019-01-01 04:24

Others may have a better solution, but my solution has been to have a simple container like VBox in the outer fxml, then load the new content and add it as a child of the container. If you're only loading one or two forms, this might be the way go to go. However, for a more complete framework, I found this blog post helpful: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1 She has source code for her framework which includes fancy transitions. Although it's meant to manage top level scenes, I found it easy to adapt for managing inner content regions too.

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 04:28

My example of the mask.

Using:

Main.getNavigation().load(View2.URL_FXML).Show();
Main.getNavigation().GoBack();
查看更多
萌妹纸的霸气范
6楼-- · 2019-01-01 04:30

If you're looking for a way to make the button call the new fxml file, this worked for me.

@FXML
private void mainBClicked(ActionEvent event) throws IOException {
    Stage stage;
    Parent root;
    stage=(Stage) ((Button)(event.getSource())).getScene().getWindow();
    root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
查看更多
大哥的爱人
7楼-- · 2019-01-01 04:34

Got stuck up in this too Tried out most of the answers, wasn't what I wanted so I just used the ideals given to do this:

public class Main extends Application {
public static Stage homeStage;
@Override
public void start(Stage primaryStage) throws Exception{
    homeStage = primaryStage;
    Parent root = FXMLLoader.load(getClass().getResource("mainView.fxml"));
    root.getStylesheets().add(getClass().getResource("stylesheet/custom.css").toExternalForm());
    homeStage.setTitle("Classification of Living Organisms");
    homeStage.setScene(new Scene(root, 600, 500));
    homeStage.show();
}


public static void main(String[] args) {
    launch(args);
  }
}

this is my main class. Main.java with the landing window/page mainView.fxml. Used a little of @Tomasz idea, although confused me a lil before I did this in my mainController.java class:

public void gotoSubMenu(Event event) {
    Parent window1;
    try {
        window1 = FXMLLoader.load(getClass().getResource("src/displayView.fxml"));
        Stage window1Stage;
        Scene window1Scene = new Scene(window1, 600, 500);
        window1Stage = Main.homeStage;
        window1Stage.setScene(window1Scene);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

created a new Parent window called 'window1' that loaded the second fxml file called 'displayView.fxml' in the src directory. created an object of the main view stage and set the scene to the newly created scene whose root is window1. Hope this helps the ones coming into #JavaFX now.

查看更多
登录 后发表回答