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.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getSide() | Returns the current position of tabs on the TabPane |
| getTabs() | Returns the tabs of the TabPane. |
| setSide(Side v) | The position to place the tabs in this TabPane. |
| setSelectionModel(SingleSelectionModel v) | Sets the model used for tab selection. |
| getSelectionModel() | Returns the selection model for tab selection. |
| getTabMaxHeight() | Returns the max height of the tab in TabPane. |
| getTabMinHeight() | Returns the min height of the tab in TabPane. |
| getTabMaxWidth() | Returns the max Width of the tab in TabPane. |
| getTabMinWidth() | Returns the min Width of the tab in TabPane. |
| setTabMaxHeight(double v) | Sets the max height of the tab in TabPane. |
| setTabMinHeight(double v) | Sets the min height of the tab in TabPane. |
| setTabMaxWidth(double v) | Sets the max width of the tab in TabPane. |
| setTabMinWidth(double v) | Sets the min width of the tab in TabPane. |
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 TabPaneimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.control.*;ÂÂpublicclassTabPane_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("Creating Tab");       Â// create a tabpane       ÂTabPane tabpane =newTabPane();       Â// create multiple tabs       Âfor(inti =0; i <10; i++) {           Â// create Tab           ÂTab tab =newTab("Tab_"+ (int)(i +1));           Â// create a label           ÂLabel label =newLabel("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 =newScene(tabpane,600,500);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(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 tabsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.control.*;importjavafx.event.Event;importjavafx.event.EventHandler;ÂÂpublicclassTabPane_2extendsApplication {ÂÂ// counter of tabsintcounter =0;ÂÂ// launch the applicationpublicvoidstart(Stage stage){   Â// set title for the stage   Âstage.setTitle("Creating Tab");   Â// create a tabpane   ÂTabPane tabpane =newTabPane();   Âfor(inti =0; i <5; i++) {       Â// create Tab       ÂTab tab =newTab("Tab_"+ (int)(counter +1));       Â// create a label       ÂLabel label =newLabel("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 =newTab();   Â// action event   ÂEventHandler<Event> event =   ÂnewEventHandler<Event>() {       Âpublicvoidhandle(Event e)       Â{           Âif(newtab.isSelected())           Â{               Â// create Tab               ÂTab tab =newTab("Tab_"+ (int)(counter +1));               Â// create a label               ÂLabel label =newLabel("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 =newScene(tabpane,600,500);   Â// set the scene   Âstage.setScene(scene);   Âstage.show();}ÂÂ// Main Methodpublicstaticvoidmain(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
