简体   繁体   中英

JavaFX, Open a Screen in same Window

I'm trying to implement an application, which has a simple navigation. One Main Menu, 3 Submenus, with another 3 Submenus each.

I need the application open every submenu recursively in the same Window with the Mainmenu as the root screen. I must be able to return to that menu by going via the "Back" Button on each Submenu.

I implemented a Main class, a Controller Class and a FXML-file for EACH (!) Menu and Submenu.

Eg my Main Menu

      package application;

import org.apache.log4j.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class Main extends Application {

    // Initialize Logger
    private static final Logger logger = Logger.getLogger(Main.class);

    @Override
    public void start(Stage primaryStage)
    {
        try
        {
            AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("MainFrame.fxml"));
            Scene scene = new Scene(root,1000,500);

            primaryStage.setScene(scene);
            primaryStage.show();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        logger.info("Starting application.");
        launch(args);
    }
}

My MainController

    package application;

import org.apache.log4j.Logger;


import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class MainFrameController
{

    private static final Logger logger = Logger.getLogger(MainFrameController.class);

    @FXML
    private Button btn_random1;
    @FXML
    private Button btn_random2;
    @FXML
    private Button btn_random3;
    @FXML
    private Button btn_random4;

    public void initialize()
    {
        //mainService = new MainService();
    }

    @FXML
    private void onRandomButton1() throws Exception
    {
        logger.info("onRandomButton1Clicked");

        Stage stage = new Stage();
        AnchorPane root;
        root = (AnchorPane)FXMLLoader.load(getClass().getResource("RandomFXML1.fxml"));
        Scene scene = new Scene(root,1000,500);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void onRandomButton2()
    {
        logger.info("onRandomButton1");
    }

    @FXML
    private void onRandomButton3()
    {
        logger.info("onRandomButton2");

    }
    @FXML
    private void onRandomButton4()
    {
        Platform.exit();
        logger.info("onRandomButton3");
    }
}

Is there a way to simply change my code, so it does open in the same window?

I took a look at several tutorials with relatively complex ways of solving this, I'd like to stick to my code and not changing too much, otherwise I'd have to start all over again.

Pls note, that this is only one of many Main/Controller/FXML combinations, I have about 10 screens and "subscreens", which are being navigated like this (by java opening a new window).

Ideas anyone? Or maybe a relatively simple tutorial (for which I dont have to change my whole code)?

Thanks!

Have an empty controller at the root (or perhaps with a single empty anchorpane) and have it open the other controllers and add it to the current pane?

I currently have a similar setup but with a tab pane: each module is loaded into a separate tab. Each module itself has an fxml file, a controller etc. The core code dynamically creates new tabs etc for each module and loads them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM