Urle
Urle

Reputation: 131

Dynamically adding AnchorPane defined in FXML to Tabs and getting later access to this Pane content

I would like to add TableView dynamically to tabs. Now I would like to have access to controllers. I don't know how to do this. Here is code:

sample.fxml code:

<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <TabPane fx:id="tabPane">
    </TabPane>
</GridPane>

Controller:

public class Controller {

    @FXML
    private TabPane tabPane;

    public TabPane getTabPane() {
        return tabPane;
    }

    public void setTabPane(TabPane tabPane) {
        this.tabPane = tabPane;
    }
}

FXMLTableView.fxml code:

<AnchorPane xmlns="http://javafx.com/javafx"
        xmlns:fx="http://javafx.com/fxml"
        fx:controller="sample.ControllerFXMLTableView"
        prefHeight="400.0" prefWidth="600.0">

    <TableView>
        <columns>
            <TableColumn fx:id="columnName" text="name"/>
            <TableColumn text="surname"/>
        </columns>
    </TableView>
</AnchorPane>

and controller:

public class ControllerFXMLTableView {

    @FXML
    private TableColumn columnName;

    public ControllerFXMLTableView() {}

    public TableColumn getColumnName() {
        return columnName;
    }

    public void setColumnName(TableColumn columnName) {
        this.columnName = columnName;
    }
}

And in Main.java I have this code:

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));
        Parent root = loaderSample.load();

        Controller controller = loaderSample.getController();

        TabPane tabPane = controller.getTabPane();

        List<String> listMonths = List.of("January", "February", "March", "April");

        listMonths.forEach(itemList -> {
            try {
                Tab tabTemp = new Tab(itemList.toString());
                AnchorPane anchorPane = FXMLLoader.load(getClass().getResource("FXMLTableView.fxml"));
                tabTemp.setContent(anchorPane);
                tabPane.getTabs().add(tabTemp);
            } catch(Exception ex){}
        });


        stage.setTitle("Hello World");
        stage.setScene(new Scene(root, 300, 275));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Not sure this is the proper way I should do it. Would like to know the acceptable splution. Now I would like to have access to column marked with fx:id="columnName". How to get access to ControllerFXMLTableView for each tab?

12 months is not too many to just code it in fxml (and have no problem with dynamic code), but this just asks for a loop.

Upvotes: 1

Views: 789

Answers (1)

c0der
c0der

Reputation: 18812

How to get access to ControllerFXMLTableView for each tab ?

Simply do it within the forEach loop:

 FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLTableView.fxml"));
 AnchorPane anchorPane = loader.load();
 ControllerFXMLTableView tabController = loader.getController();
 TableColumn column = tabController.getColumnName();

Edit: to sore refences for later use you can define an appropriate container, a Map<String,TableColumn> columns = new HashMap<>(); for example, and use it: columns.put(itemList, tabController.getColumnName());

Upvotes: 2

Related Questions