How to get stage from JavaFX Application Class

I am writing an application with two different BorderPanes, BorderPane A and BorderPane B. The application has 2 menu items, so when you click on it it should show BorderPane A or BorderPane B.

This is the application class on which I want to use

public class SampleApp extends Application {
private Stage primaryStage;

public static void main(final String[] args) {
    launch(args);
}    
@Override
public void start(final Stage stage)
        throws Exception {
    final AnnotationConfigApplicationContext context = new  AnnotationConfigApplicationContext(SampleAppFactory.class);
    final SpringFxmlLoader loader = new SpringFxmlLoader(context);      
    final Parent root = (Parent) loader.load("Main.fxml", Main.class);
    final Scene scene = new Scene(root, 600, 480, Color.ALICEBLUE);
    this.primaryStage = stage;
    scene.getStylesheets().add("fxmlapp.css");
    stage.setScene(scene);
    stage.show();
}
}

There are two BorderPanes in Main.java class when menuItem is selected. I want to show borderpane in the application.

Does anyone know how to show Borderpane (set the scene on stage) from this method ( showBorderPane )? I would like to get the scene and set the scene using borderpane:

public class Main extends BorderPane implements Initializable {

@FXML
private Verbinding verbindingContent; // BORDERPANE A
@FXML
private Beheer beheerContent;// BORDERPANE A
@FXML
private MenuBar menuContent;

@Override
public void initialize(final URL url, final ResourceBundle rb) {
    System.out.println(url);
    menuContent.setFocusTraversable(true);
}

@FXML
private void showBorderPane(final ActionEvent event) {
    final MenuItem menuItem = (MenuItem) event.getSource();
}


@FXML
private void handleCloseAction(final ActionEvent event) {
    System.exit(0);
}

}

my Main.xml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.effect.*?>
<?import nl.mamaloe.tab.view.Main?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="nl.mamaloe.tab.view.Main">
<fx:define>     
    <fx:include source="scene/Beheer.fxml" fx:id="beheerContent" />
    <!-- fx:include source="Menu.fxml" fx:id="menuContent" / -->
</fx:define>
<top>
    <MenuBar fx:id="menuContent" onMouseClicked="#handleKeyInput">
        <menus>
            <Menu text="Systeem">
                <items>
                    <MenuItem text="Verbinding maken" onAction="#handleVerbindingAction"/>
                    <SeparatorMenuItem />
                    <MenuItem text="Afsluiten"  onAction="#handleCloseAction"/>
                </items>
            </Menu>

            <Menu text="TAB">
                <items>
                    <MenuItem text="Script toevoegen" onAction="#handleAboutAction"/>
                    <SeparatorMenuItem />
                    <MenuItem text="Script draaien" />                      
                </items>
            </Menu>
            <Menu text="About" onAction="#handleAboutAction">
                <items>
                    <MenuItem text="Op basis van wizard" />
                </items>
            </Menu>
        </menus>
    </MenuBar>
</top> 
<center>   <Label text="Add New Dock of Home" />

</center>

I saw that this can be done using the application launch method. But I would like to implement it in Main.java because of the structure, and I mainly use FXML to declare a GUI.

+3
1

"Main.fxml" FXMLLoader, BorderPane ( Main.fxml). / , BorderPane . , BorderPanes. , FXML, :

<center>
    <StackPane fx:id="pane">
        <children>
              <Label text="Add New Dock of Home" />
        </children>
    </StackPane>
</center>

StackPane /, . :

@FXML
private StackPane pane;

"extends BorderPane", . ,

@FXML
private void showBorderPane(final ActionEvent event) {
    final MenuItem menuItem = (MenuItem) event.getSource();
    pane.getChildren().clear(); // Clear old content.
    switch (menuItem.getText()) {
        case "Borderpane A":
            pane.getChildren().add(borderPaneA);
            break;
        case "Borderpane B":
            pane.getChildren().add(borderPaneB);
            break;
       default:
            pane.getChildren().add(new Label("Add New Dock of Home"));
    }
}
+1

All Articles