JavaFX | Tab Class

Tab class is a part of JavaFX. Tab class creates an object that is contained in TabPane. A Tab can contain any node as its content. A TabPane can contain multiple tabs. When the user clicks on the tab the content of the tab is made visible to the user.
Constructors of the class:
- Tab(): Creates a new empty tab.
- Tab(String t): Creates a new tab with specified title.
- Tab(String t, Node c): Creates a new tab with specified title and content.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getContent() | Returns the content node of the Tab. |
| getContextMenu() | Returns the context menu associated with the tab. |
| getGraphic() | Returns the graphic of the tab. |
| getId() | Return the ID of the tab. |
| getStyle() | The CSS style string associated to this tab. |
| getTabPane() | Returns the tab pane of the tab. |
| getText() | Returns the text shown in the tab. |
| getTooltip() | Returns the tooltip associated with the tab. |
| setId(String v) | Sets the ID of the tab. |
| setContent(Node v) | Sets the content for the tab. |
| setContextMenu(ContextMenu v) | Sets the context menu for the tab. |
| setGraphic(Node v) | Set the graphic for the node. |
| setStyle(String v) | Sets the string representation of the CSS style associated with this tab. |
| setText(String v) | Sets the text for the tab |
| setTooltip(Tooltip v) | Sets the tooltip for the tab (a popup appears when the user hovers over the tab). |
| setDisable(boolean v) | States the disabled state of this tab. |
Below programs illustrate the use of Tab class:
- Java program to create a tab and add it to the TabPane: In this program we will create a Tab named tab_1. 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. We will create a TabPane named tabpane and add the tab to the tabpane. Now, we will 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 a tab// and add it to the TabPaneimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.control.*;publicclassTab_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating Tab");// create TabTab tab_1 =newTab("Tab_1");// create a labelLabel label =newLabel(" This is a tab ");// add label to the tabtab_1.setContent(label);// add tab// create a tabpaneTabPane tabpane =newTabPane(tab_1);// create a sceneScene scene =newScene(tabpane,600,500);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to create a tab, add graphic(in the tab) to it and add it to the TabPane: In this program we will create a Tab named tab_1. We will also create a Label named label. We will add label to the tab by using the function setContent(). The title of the tab will be passed as arguments. We will create a FileInputStream named input to import the image. An Image will be created named image from the file input stream, then we will create an ImageView named imageview from the imported image. We will add this imageview to the tab using the setGraphic() function. We will create a TabPane named tabpane and add the tab to the tabpane . Now, we will 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 a tab, add// graphic(in the tab) to it and add// it to the TabPaneimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.control.*;importjava.io.*;importjavafx.scene.image.*;publicclassTab_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating Tab");// create TabTab tab_1 =newTab("Tab_1");// create a labelLabel label =newLabel(" This is a tab ");// add label to the tabtab_1.setContent(label);// create a input streamFileInputStream input =newFileInputStream("f:\\gfg.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// add graphic to the tabtab_1.setGraphic(imageview);// add tab// create a tabpaneTabPane tabpane =newTabPane(tab_1);// create a sceneScene scene =newScene(tabpane,600,500);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.err.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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/Tab.html




