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.

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to create a tab

// and add it to the TabPane

importjavafx.application.Application;

importjavafx.scene.Scene;

importjavafx.scene.layout.*;

importjavafx.stage.Stage;

importjavafx.scene.Group;

importjavafx.scene.control.*;

publicclassTab_1 extendsApplication {

// launch the application

publicvoidstart(Stage stage)

{

// set title for the stage

stage.setTitle("creating Tab");

// create Tab

Tab tab_1 = newTab("Tab_1");

// create a label

Label label = newLabel(" This is a tab ");

// add label to the tab

tab_1.setContent(label);

// add tab

// create a tabpane

TabPane tabpane = newTabPane(tab_1);

// 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);

}

}

chevron_right

filter_none

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.

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to create a tab, add

// graphic(in the tab) to it and add

// it to the TabPane

importjavafx.application.Application;

importjavafx.scene.Scene;

importjavafx.scene.layout.*;

importjavafx.stage.Stage;

importjavafx.scene.Group;

importjavafx.scene.control.*;

importjava.io.*;

importjavafx.scene.image.*;

publicclassTab_2 extendsApplication {

// launch the application

publicvoidstart(Stage stage)

{

try{

// set title for the stage

stage.setTitle("creating Tab");

// create Tab

Tab tab_1 = newTab("Tab_1");

// create a label

Label label = newLabel(" This is a tab ");

// add label to the tab

tab_1.setContent(label);

// create a input stream

FileInputStream input = newFileInputStream("f:\\gfg.png");

// create a image

Image image = newImage(input);

// create a image View

ImageView imageview = newImageView(image);

// add graphic to the tab

tab_1.setGraphic(imageview);

// add tab

// create a tabpane

TabPane tabpane = newTabPane(tab_1);

// create a scene

Scene scene = newScene(tabpane, 600, 500);

// set the scene

stage.setScene(scene);

stage.show();

}

catch(Exception e) {

System.err.println(e.getMessage());

}

}

// Main Method

publicstaticvoidmain(String args[])

{

// launch the application

launch(args);

}

}

chevron_right

filter_none

Output:

Note: The above programs might not run in an online IDE. Please use an offline compiler.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.