Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Javafx- Display fxml document in BorderPane of another

7d1b7fec-04e8-4af9-84c4-9f438f73986dApr 30 2017 — edited May 3 2017

Hello guys,

I have gone mad because of this issue. I want to design a desktop app where I have a main window with side pane containing buttons. When a button is clicked another fxml document displays in the center of the main window (which is basically a Border Pane). Another thing, how can I put a close button on the sub window to close the displayed fmxl document? The code runs and displays the main window content, but crashes when I click on the button to display Page1. This is very basic code. I don't need complex solutions please.

Two pictures of what I need to design are attached.

Here is the code in my Main Class:

package main;

import java.io.IOException;

import javafx.application.Application;

import javafx.fxml.FXMLLoader;

import javafx.scene.Scene;

import javafx.stage.Stage;

public class Main extends Application {

private Stage primaryStage;

@Override

public void start(Stage primaryStage) throws Exception {

    this.primaryStage = primaryStage;

    showRootStage();

}

public static void main(String\[\] args) {

    launch(args);

}

private void showRootStage() throws IOException {

    FXMLLoader loader = new FXMLLoader();

    loader.setLocation(Main.class.getResource("Stage.fxml"));

    Scene scene = new Scene(loader.load());

    primaryStage.setScene(scene);

    primaryStage.show();

}

This is the code in the main window controller (Stage document)

package main;

import java.io.IOException;

import java.net.URL;

import java.util.ResourceBundle;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.fxml.FXMLLoader;

import javafx.fxml.Initializable;

import javafx.scene.layout.BorderPane;

public class Stage implements Initializable {

@FXML

private BorderPane rootStage;

@FXML

private void handleButtonAction(ActionEvent event) throws IOException {

    FXMLLoader loader = new FXMLLoader();

    loader.setLocation(javafx.stage.Stage.class.getResource("Page1.fxml"));

    BorderPane newScene = loader.load();

    rootStage.setCenter(newScene);

}

@Override

public void initialize(URL url, ResourceBundle rb) {

    // TODO

}    

}

01.jpg02.jpg

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 31 2017
Added on Apr 30 2017
2 comments
4,925 views