0

I am basically new to JavaFX and I started my first project in it. First of all I want to ensure you that I have read articles linked below:

JavaFx TabPane : One controller per Tab JavaFX Pane inside of a Tab

I know how to put Pane directly into the Tab, but what I want is to have each Controller for every content inside Tabs in my TabPane.

Main.fxml

<center>
    <TabPane fx:id="mainTabPane">
        <tabs>
        <Tab fx:id="tabChar" closable="false" text="Character" onSelectionChanged="#showCharacterTab"/>
        <Tab fx:id="tabSkills" closable="false" text="Skills" />
        <Tab fx:id="tabMagic" closable="false" text="Magic" />
        <Tab fx:id="tabStory" closable="false" text="Story" />
        <Tab fx:id="tabPortrait" closable="false" text="Portrait" />
        <Tab fx:id="tabDatabase" closable="false" text="Char-Base" />
        </tabs>
    </TabPane>
</center>

What I have to mention that my TabPane is located in the center of BorderPane, which is a Main Window.

I tried to build new Scene for that tab and associate it withing showCharacterTab method, but at the moment I ended up with this:

@FXML
private Tab tabChar;

@FXML
    public void showCharacterTab() throws IOException{

        if (tabChar.isSelected()) {
            AnchorPane characterPane = FXMLLoader.load(getClass().getResource("character.fxml"));
            tabChar.setContent(characterPane.getParent());
        }
    }

And for now I'm totally clueless how to do it. I am a newbie and I started probably with too much content. I don't know if I have to put it in initialize() method to make it visible at start and I don't know if I use correctly onSelectionChanged method.

So to sum up: I want to make every content in different tabs associated with new FXML file and new Controller. I tried to do it avoiding Nested Controllers option described here: Nested Controllers

Coen92
  • 1
  • *"I want different content for my tabs with different controllers."*; *"I tried to do it avoiding Nested Controllers option"* Either you use one fxml+controller per tab or you different controller or not; There's no way to have both... The only thing you could do is creating the tabs from code. You shouldn't (re)load the content of the tabs on a change of the selection. Furthermore since the result of loading the fxml file in `showCharacterTab` is a node that isn't added as child to some parent, `characterPane.getParent()` ***always*** yields `null`... – fabian Mar 14 '19 at 14:37
  • Yes, of course, I must wrote that wrong. I naturally want a pair controller+fxml associated together per tab. I just want to make it work with tab - new fxml nested in tab with its controller. So instead of creating tabs in main.fxml I have to use Controller class to create them and then connect them with new FXMLLoader? – Coen92 Mar 14 '19 at 15:01
  • 1
    You've got the option of injecting using `` and `@FXML private Content1Controller content1Controller;` in the controller, but you could of course also do something like `for (URL location : tabContentLocations) { Tab tab = new Tab(); FXMLLoader loader = new FXMLLoader(location); tab.setContent(loader.load()); ...loader.getController()....; tabPane.getTabs().add(tab); }`... – fabian Mar 14 '19 at 15:11

0 Answers0