user3505901 user3505901 - 1 year ago 218
Java Question

How to create tabs dynamically in JavaFX using FXML?

How do you create a new tab using JavaFX/FXML? I've created a tabpane in my FXML but I want to click a button that causes a new tab to come up.

Here is my FXML:

<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="" xmlns:fx="" fx:controller="sample.Controller">
<MenuBar BorderPane.alignment="CENTER">
<Menu mnemonicParsing="false" text="File">
<MenuItem mnemonicParsing="false" text="New..." onAction="#btnNew"/>
<MenuItem mnemonicParsing="false" text="Save..." />
<MenuItem mnemonicParsing="false" text="Save As..." />
<MenuItem mnemonicParsing="false" text="Open..." />
<MenuItem mnemonicParsing="false" text="Close" />

<Menu mnemonicParsing="false" text="Help">
<MenuItem mnemonicParsing="false" text="About Blank" onAction="#btnAbout"/>
<TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER" fx:id="tabPane">
<Tab text="Untitled Tab 1">
<TextFlow maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" />

Here is my code that I'm trying right now in my controller:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.util.ResourceBundle;

public class Controller implements Initializable {
private Button btnAbout;
private Button btnNew;
private TabPane tabPane;

//tab array
int intTabs = 0;
Tab[] openTabs;

private void btnNew(ActionEvent event){
openTabs = new Tab[intTabs];
tabAdder(intTabs, openTabs);
}catch(Exception e){


private TabPane tabAdder(int tabNum, Tab[] tabs){
return tabPane;

private void btnAbout(ActionEvent event){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Blank: Information");
alert.setContentText("This is Blank, a simple and soon to be elegant cross platform text editor.");

public void initialize(URL url, ResourceBundle rb){



I'm not really sure what the problem is I've been going through a bunch of resources and I know how to add a regular tab.

I know that if I make a tab like this:

Tab fapTab = new Tab();

I can add it like this:


But I want to create them dynamically because I don't know how many tabs my user's would want. So my approach was to create an array of tabs that gets bigger whenever a user clicks "new" and then have a function that adds that new tab to the gui. It's not working though and I've tried several different approaches, adapting people's code to my own.

So my question is does anyone know what I'm doing wrong?

Here are some of those sources:
javafx open a new fxml file in new tab dynamically

Answer Source

You have already given the answer: just create a new tab and add it to the tab pane:

Tab tab = new Tab();

Complete example:


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>

<BorderPane xmlns:fx=""
        <TabPane fx:id="tabPane" />
        <HBox alignment="center">
            <Button text="Add tab" onAction="#addTab" />
            <Button text="List tabs" onAction="#listTabs" />

import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

public class AddTabsController  {

    private TabPane tabPane ;

    public void initialize() {
        tabPane.getTabs().add(new Tab("Tab 1"));

    private void addTab() {
        int numTabs = tabPane.getTabs().size();
        Tab tab = new Tab("Tab "+(numTabs+1));

    private void listTabs() {
        tabPane.getTabs().forEach(tab -> System.out.println(tab.getText()));

Application (

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class AddTabsExample extends Application {

    public void start(Stage primaryStage) throws Exception {
        BorderPane root = FXMLLoader.load(getClass().getResource("AddTabsExample.fxml"));
        primaryStage.setScene(new Scene(root, 800, 600));;

    public static void main(String[] args) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download