JavaFX | TabPane Class

31 Aug, 2018
TabPane class is a part of JavaFX. TabPane allows switching between several tabs. TabPane acts as a container of tabs. The positions of the tabs can be specified using the setSide() function. when the tabs do not fit in the TabPane, a menu button appears at the upper right corner of TabPane which displays all the tabs of the Tabpane.
Constructors of the class:
TabPane(): Creates a new TabPane with no tabs.TabPane(Tab... t): Creates a new TabPane with specified tabs.
TabPane(): Creates a new TabPane with no tabs.
TabPane(Tab... t): Creates a new TabPane with specified tabs.
Commonly Used Methods:
Below programs illustrate the use of TabPane class:
Java program to create multiple tabs and add it to the TabPane: In this program we will create a Tabpane named tabpane. To add multiple tabs we will use a for loop and then add tabs to the tabpane. Create a Tab named tab. We will also create a Label named label. We will add the label to the tab by using the function setContent(). The title of the tab will be passed as arguments. Now create a TabPane named tabpane and add the tab to the tabpane. After that add the tabpane to the scene and add the scene to the stage and display the stage using the show() function.// Java program to create multiple tabs // and add it to the TabPaneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;  public class TabPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Tab");          // create a tabpane        TabPane tabpane = new TabPane();          // create multiple tabs        for (int i = 0; i < 10; i++) {              // create Tab            Tab tab = new Tab("Tab_" + (int)(i + 1));              // create a label            Label label = new Label("This is Tab: " + (int)(i + 1));              // add label to the tab            tab.setContent(label);              // add tab            tabpane.getTabs().add(tab);        }          // create a scene        Scene scene = new Scene(tabpane, 600, 500);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/TabPane_1.mp400:0000:0000:33Use Up/Down Arrow keys to increase or decrease volume.Java program to create multiple tabs and add it to the TabPane and also create a tab which on selected will create new tabs: In this program we will create a Tabpane named tabpane. We will add multiple tabs to the tabpane. To add multiple tabs we will use a for loop. We will create a Tab named tab. Now create a Label named label. We will add a label to the tab by using the function setContent(). We will also create a tab named newtab. When it is selected then it will create a new tab. Add event handler to the tab by using setOnSelectionChanged() function. The event handler will create a new tab and add it before the new tab in tabpane using getTabs().add() function and select the last tab using the getSelectionModel().select() function. The title of the tab will be passed as arguments. We will create a TabPane named tabpane and add the tab to the tabpane and add the tabpane to the scene and scene to the stage. Display the stage using the show() function.// Java program to create multiple tabs and// add it to the tabPane and also create a // tab which on selected will create new tabsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;import javafx.event.Event;import javafx.event.EventHandler;  public class TabPane_2 extends Application {  // counter of tabsint counter = 0;  // launch the applicationpublic void start(Stage stage){      // set title for the stage    stage.setTitle("Creating Tab");      // create a tabpane    TabPane tabpane = new TabPane();      for (int i = 0; i < 5; i++) {          // create Tab        Tab tab = new Tab("Tab_" + (int)(counter + 1));          // create a label        Label label = new Label("This is Tab: "                         + (int)(counter + 1));          counter++;          // add label to the tab        tab.setContent(label);          // add tab        tabpane.getTabs().add(tab);    }      // create a tab which    // when pressed creates a new tab    Tab newtab = new Tab();      // action event    EventHandler<Event> event =     new EventHandler<Event>() {          public void handle(Event e)        {            if (newtab.isSelected())             {                  // create Tab                Tab tab = new Tab("Tab_" + (int)(counter + 1));                  // create a label                Label label = new Label("This is Tab: "                                  + (int)(counter + 1));                  counter++;                  // add label to the tab                tab.setContent(label);                  // add tab                tabpane.getTabs().add(                        tabpane.getTabs().size() - 1, tab);                  // select the last tab                tabpane.getSelectionModel().select(                        tabpane.getTabs().size() - 2);            }        }    };      // set event handler to the tab    newtab.setOnSelectionChanged(event);      // add newtab    tabpane.getTabs().add(newtab);      // create a scene    Scene scene = new Scene(tabpane, 600, 500);      // set the scene    stage.setScene(scene);      stage.show();}  // Main Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/TabPane_2.mp400:0000:0000:28Use Up/Down Arrow keys to increase or decrease volume.
Java program to create multiple tabs and add it to the TabPane: In this program we will create a Tabpane named tabpane. To add multiple tabs we will use a for loop and then add tabs to the tabpane. Create a Tab named tab. We will also create a Label named label. We will add the label to the tab by using the function setContent(). The title of the tab will be passed as arguments. Now create a TabPane named tabpane and add the tab to the tabpane. After that add the tabpane to the scene and add the scene to the stage and display the stage using the show() function.// Java program to create multiple tabs // and add it to the TabPaneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;  public class TabPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Tab");          // create a tabpane        TabPane tabpane = new TabPane();          // create multiple tabs        for (int i = 0; i < 10; i++) {              // create Tab            Tab tab = new Tab("Tab_" + (int)(i + 1));              // create a label            Label label = new Label("This is Tab: " + (int)(i + 1));              // add label to the tab            tab.setContent(label);              // add tab            tabpane.getTabs().add(tab);        }          // create a scene        Scene scene = new Scene(tabpane, 600, 500);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/TabPane_1.mp400:0000:0000:33Use Up/Down Arrow keys to increase or decrease volume.
// Java program to create multiple tabs // and add it to the TabPaneimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;  public class TabPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Tab");          // create a tabpane        TabPane tabpane = new TabPane();          // create multiple tabs        for (int i = 0; i < 10; i++) {              // create Tab            Tab tab = new Tab("Tab_" + (int)(i + 1));              // create a label            Label label = new Label("This is Tab: " + (int)(i + 1));              // add label to the tab            tab.setContent(label);              // add tab            tabpane.getTabs().add(tab);        }          // create a scene        Scene scene = new Scene(tabpane, 600, 500);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java program to create multiple tabs and add it to the TabPane and also create a tab which on selected will create new tabs: In this program we will create a Tabpane named tabpane. We will add multiple tabs to the tabpane. To add multiple tabs we will use a for loop. We will create a Tab named tab. Now create a Label named label. We will add a label to the tab by using the function setContent(). We will also create a tab named newtab. When it is selected then it will create a new tab. Add event handler to the tab by using setOnSelectionChanged() function. The event handler will create a new tab and add it before the new tab in tabpane using getTabs().add() function and select the last tab using the getSelectionModel().select() function. The title of the tab will be passed as arguments. We will create a TabPane named tabpane and add the tab to the tabpane and add the tabpane to the scene and scene to the stage. Display the stage using the show() function.// Java program to create multiple tabs and// add it to the tabPane and also create a // tab which on selected will create new tabsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;import javafx.event.Event;import javafx.event.EventHandler;  public class TabPane_2 extends Application {  // counter of tabsint counter = 0;  // launch the applicationpublic void start(Stage stage){      // set title for the stage    stage.setTitle("Creating Tab");      // create a tabpane    TabPane tabpane = new TabPane();      for (int i = 0; i < 5; i++) {          // create Tab        Tab tab = new Tab("Tab_" + (int)(counter + 1));          // create a label        Label label = new Label("This is Tab: "                         + (int)(counter + 1));          counter++;          // add label to the tab        tab.setContent(label);          // add tab        tabpane.getTabs().add(tab);    }      // create a tab which    // when pressed creates a new tab    Tab newtab = new Tab();      // action event    EventHandler<Event> event =     new EventHandler<Event>() {          public void handle(Event e)        {            if (newtab.isSelected())             {                  // create Tab                Tab tab = new Tab("Tab_" + (int)(counter + 1));                  // create a label                Label label = new Label("This is Tab: "                                  + (int)(counter + 1));                  counter++;                  // add label to the tab                tab.setContent(label);                  // add tab                tabpane.getTabs().add(                        tabpane.getTabs().size() - 1, tab);                  // select the last tab                tabpane.getSelectionModel().select(                        tabpane.getTabs().size() - 2);            }        }    };      // set event handler to the tab    newtab.setOnSelectionChanged(event);      // add newtab    tabpane.getTabs().add(newtab);      // create a scene    Scene scene = new Scene(tabpane, 600, 500);      // set the scene    stage.setScene(scene);      stage.show();}  // Main Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/TabPane_2.mp400:0000:0000:28Use Up/Down Arrow keys to increase or decrease volume.
// Java program to create multiple tabs and// add it to the tabPane and also create a // tab which on selected will create new tabsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.Group;import javafx.scene.control.*;import javafx.event.Event;import javafx.event.EventHandler;  public class TabPane_2 extends Application {  // counter of tabsint counter = 0;  // launch the applicationpublic void start(Stage stage){      // set title for the stage    stage.setTitle("Creating Tab");      // create a tabpane    TabPane tabpane = new TabPane();      for (int i = 0; i < 5; i++) {          // create Tab        Tab tab = new Tab("Tab_" + (int)(counter + 1));          // create a label        Label label = new Label("This is Tab: "                         + (int)(counter + 1));          counter++;          // add label to the tab        tab.setContent(label);          // add tab        tabpane.getTabs().add(tab);    }      // create a tab which    // when pressed creates a new tab    Tab newtab = new Tab();      // action event    EventHandler<Event> event =     new EventHandler<Event>() {          public void handle(Event e)        {            if (newtab.isSelected())             {                  // create Tab                Tab tab = new Tab("Tab_" + (int)(counter + 1));                  // create a label                Label label = new Label("This is Tab: "                                  + (int)(counter + 1));                  counter++;                  // add label to the tab                tab.setContent(label);                  // add tab                tabpane.getTabs().add(                        tabpane.getTabs().size() - 1, tab);                  // select the last tab                tabpane.getSelectionModel().select(                        tabpane.getTabs().size() - 2);            }        }    };      // set event handler to the tab    newtab.setOnSelectionChanged(event);      // add newtab    tabpane.getTabs().add(newtab);      // create a scene    Scene scene = new Scene(tabpane, 600, 500);      // set the scene    stage.setScene(scene);      stage.show();}  // Main Methodpublic static void main(String args[]){      // launch the application    launch(args);}}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TabPane.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Stack Class in Java