-1

I have a TabPane with two Tabs and I want to load another FXML when I click on one Tab

<TabPane fx:id="tabPaneGet" layoutY="-7.0" prefHeight="30.0" prefWidth="560.0" tabClosingPolicy="UNAVAILABLE">
              <tabs>
                <Tab disable="true" text="Получить сообщение из очереди">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                  </content>
                </Tab>
                <Tab text="Отправить сообщение в очередь" fx:id="tabPaneSendMessage">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                  </content>
                </Tab>
              </tabs>
            </TabPane>

Class start Application

public class TestFrame extends Application {
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    URL xmlUrl = getClass().getResource("/Test.fxml");
    loader.setLocation(xmlUrl);
    Parent root = loader.load();
    stage.setTitle("Title");
    stage.setScene(new Scene(root));
    stage.show();
}

}

Maybe something needs to be added to the controller? And if so, where and what?

Vanish
  • 57
  • 6
  • 1
    I just wonder why you want to do this; specifically why you want to wait for a tab to be selected to load its content. This might create a poor UX as there will potentially be slight pauses before the UI appears after the user changes tabs. Why not just load the content for all the tabs immediately? – James_D May 16 '23 at 11:52
  • The idea is good. I just don't understand now how can I switch to another Scene if I have it in another fxml file without loading it? – Vanish May 16 '23 at 12:17
  • If you mean you have the content of each tab in a different FXML file, just use [``](https://openjfx.io/javadoc/20/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#include_elements). I don't really know what you mean by "switch to another Scene" in this context. – James_D May 16 '23 at 13:24
  • Is this what you are trying to do? https://stackoverflow.com/q/39164050/2189127 – James_D May 16 '23 at 13:35

1 Answers1

0

If you want to navigate and load another FXML on clicking on specific TabPane, you need to use the setOnSelectionChanged event handler for the desired tab.

public class ABC extends Application {

@Override
public void start(Stage primaryStage) {
    try {
        TabPane tabPane = new TabPane();

        Tab tab1 = new Tab("Tab 1");
        tab1.setOnSelectionChanged(event -> {
            if (tab1.isSelected()) {
                loadFXML("file_1.fxml", tab1);
            }
        });

        Tab tab2 = new Tab("Tab 2");
        tab2.setOnSelectionChanged(event -> {
            if (tab2.isSelected()) {
                loadFXML("file_2.fxml", tab2);
            }
        });

        tabPane.getTabs().addAll(tab1, tab2);

        Scene scene = new Scene(tabPane, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void loadFXML(String fxmlPath, Tab tab) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath));
        AnchorPane root = loader.load();

        tab.setContent(root);
    } catch (IOException e) {
        e.printStackTrace();
    }
}