Open In App

JavaFX | Tab Class

Last Updated : 31 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Tab(): Creates a new empty tab.
  2. Tab(String t): Creates a new tab with specified title.
  3. 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:

  1. 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 TabPane
    import 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 Tab_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            // set title for the stage
            stage.setTitle("creating Tab");
      
            // create Tab
            Tab tab_1 = new Tab("Tab_1");
      
            // create a label
            Label label = new Label(" This is a tab ");
      
            // add label to the tab
            tab_1.setContent(label);
      
            // add tab
            // create a tabpane
            TabPane tabpane = new TabPane(tab_1);
      
            // 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:

  2. 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 TabPane
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.control.*;
    import java.io.*;
    import javafx.scene.image.*;
      
    public class Tab_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("creating Tab");
      
                // create Tab
                Tab tab_1 = new Tab("Tab_1");
      
                // create a label
                Label label = new Label(" This is a tab ");
      
                // add label to the tab
                tab_1.setContent(label);
      
                // create a input stream
                FileInputStream input = new FileInputStream("f:\\gfg.png");
      
                // create a image
                Image image = new Image(input);
      
                // create a image View
                ImageView imageview = new ImageView(image);
      
                // add graphic to the tab
                tab_1.setGraphic(imageview);
      
                // add tab
                // create a tabpane
                TabPane tabpane = new TabPane(tab_1);
      
                // create a scene
                Scene scene = new Scene(tabpane, 600, 500);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
            catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
      
        // Main Method
        public 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/Tab.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads