Updated April 1, 2023
Introduction to JavaFX TabPane
TabPane in JavaFX is used to create tabs in standalone applications like mobile apps, PC installation software etc. TabPane class in JavaFX can create any number of tabs using TabPane() constructor. Each TabPane has a specific index position for recognizing the exact tabs. TabPane tab index positions used for switching between tabs for adding the functionalities inaccurate tabs. JavaFX TabPane class available in javafx.scene.control.TabPane package.
What can we do with TabPane in JavaFX?
TabPane in JavaFX is used for creating multiple tabs for adding different content in the tabs.
Constructors
- TabPane(): It is default constructor, used to create Tab pane instance.
- TabPane(tab1, tab2, tab3,……): It is parameterized constructor, it accepts multiple tab values at a time.
Methods
- setSide(Side s): setSide() method used to set the tab at index position.
- getSide(): getSide() method gives current position of tab.
- getTabs(): getTabs() gives the tabs from Tab pane.
- setSelectionModel(SingleSelectionModel s): setSelectionModel() method used to set the selection model of the tab pane.
- getSelectionModel(): setSelectionModel() method used to get the selection model of the tab pane.
- setTabMaxHeight(double d): setTabMaxHeight() method set the max height of the tab pane.
- getTabMaxHeight(): getTabMaxHeight() method gives you max height of the tab pane.
- setTabMinHeight(double d): setTabMinHeight() method set the min height of the tab pane.
- getTabMinHeight(): getTabMinHeight() method gives you min height of the tab pane.
- setTabMaxWidth(double d): setTabMaxHeight() method set the max width of the tab pane.
- getTabMaxWidth(): getTabMaxWidth() method gives you max width of the tab pane.
- setTabMinWidth(double d): setTabMinHeight() method set the min width of the tab pane.
- getTabMinWidth(): getTabMinWidth() method gives you max width of the tab pane.
How to Create ScrollPane in JavaFX?
1. Accessing JavaFX features user defined class must extends Application class.
2. In JavaFX creating TabPane is first step. TabPane can instantiate by using new keyword.
TabPane tabPane=new TabPane ();
- Creating Tabs and adding tabs to the TabPane is second step.
Tab tab=new Tab("Some content");
tabPane.getTabs().add(tab);
3. Create VBox or any other display(like TilePane or HBox etc. as per requirement) class to add the items is third step.
VBox vBox=new VBox (tabPane); //Gives vertical box
4. Creating a scene means screen to display output is the fourth step.
Scene screen = new Scene(vBox, length, width);
5. Adding Scene reference screen to the Stage object reference is fifth step. Adding output screen to Stage. We will get this stage object reference from start predefined JavaFX method.
stage.setScene(screen);
6. At last display output screen by showing stage reference with the show () method.
stage.show();
Examples of JavaFX TabPane
Below are the examples of JavaFX TabPane:
Example #1
Adding tabs to tab pane.
Code:
package com.tabpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TabPaneTabs extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("TabPane Tabs");
// Create a TabPane
TabPane tabPane = new TabPane();
//creating Tabs for adding content
Tab educbaTab=new Tab("EDUCBA");
Tab coursesTab=new Tab("Courses");
Tab feeTab=new Tab("Fee of Courses");
Tab aboutTab=new Tab("About EDUCBA");
//adding tabs to tabpane
tabPane.getTabs().add(educbaTab);
tabPane.getTabs().add(coursesTab);
tabPane.getTabs().add(feeTab);
tabPane.getTabs().add(aboutTab);
//creating VBox for adding tab pane
VBox vBox=new VBox(tabPane);
//adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 401);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- As you can see in the above output, we have added tabs to the tab pane with the help of VBox.
Example #2
Adding content to the tabs.
Code:
package com.tabpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AddingContentToTabs extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("Adding Content to Tabs");
// Create a TabPane
TabPane tabPane = new TabPane();
// creating label for adding content to tabs
Label educbaLabel = new Label("\t\t\tEDUCBA is online courses portal.");
Label courseLable = new Label("\t\t\tHTML || CSS || JavaScript || Java || Python");//\t is for tab space
Label feeLabel = new Label("\t\t\tPrices are between 1000/- to 5000/-");
Label aboutEDUCBALabel = new Label("\t\t\tEDUCBA has strong content to teach IT courses easily.");
// creating Tabs for adding content
Tab educbaTab = new Tab("EDUCBA");
Tab coursesTab = new Tab("Courses");
Tab feeTab = new Tab("Fee of Courses");
Tab aboutTab = new Tab("About EDUCBA");
// adding content to tabs
educbaTab.setContent(educbaLabel);
coursesTab.setContent(courseLable);
feeTab.setContent(feeLabel);
aboutTab.setContent(aboutEDUCBALabel);
// adding tabs to tabpane
tabPane.getTabs().add(educbaTab);
tabPane.getTabs().add(coursesTab);
tabPane.getTabs().add(feeTab);
tabPane.getTabs().add(aboutTab);
// creating VBox for adding tab pane
VBox vBox = new VBox(tabPane);
// adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 401);
primaryStage.setScene(scene);
// showing the output
primaryStage.show();
}
public static void main(String[] args) {
// invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- As you can see in the above output, we have added tabs, label content to the tabs and then tabs to the tab pane with the help of VBox.
Example #3
Setting minimum and maximum width and height to the tab pane.
Code:
package com.tabpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TabsHeigthWidthAlignment extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("Adding Content to Tabs");
// Create a TabPane
TabPane tabPane = new TabPane();
// creating label for adding content to tabs
Label educbaLabel = new Label("\t\t\tEDUCBA is online courses portal.");
Label courseLable = new Label("\t\t\tHTML || CSS || JavaScript || Java || Python");//\t is for tab space
Label feeLabel = new Label("\t\t\tPrices are between 1000/- to 5000/-");
Label aboutEDUCBALabel = new Label("\t\t\tEDUCBA has strong content to teach IT courses easily.");
// creating Tabs for adding content
Tab educbaTab = new Tab("EDUCBA");
Tab coursesTab = new Tab("Courses");
Tab feeTab = new Tab("Fee of Courses");
Tab aboutTab = new Tab("About EDUCBA");
//setting min and max width, height respectively
tabPane.setTabMinWidth(30);
tabPane.setTabMinHeight(30);
tabPane.setTabMaxWidth(100);
tabPane.setTabMaxHeight(100);
// adding content to tabs
educbaTab.setContent(educbaLabel);
coursesTab.setContent(courseLable);
feeTab.setContent(feeLabel);
aboutTab.setContent(aboutEDUCBALabel);
// adding tabs to tabpane
tabPane.getTabs().add(educbaTab);
tabPane.getTabs().add(coursesTab);
tabPane.getTabs().add(feeTab);
tabPane.getTabs().add(aboutTab);
// creating VBox for adding tab pane
VBox vBox = new VBox(tabPane);
// adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 401);
primaryStage.setScene(scene);
// showing the output
primaryStage.show();
}
public static void main(String[] args) {
// invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
As you can see in the above code tabPane.setTabMinWidth(30), tabPane.setTabMinHeight(30), tabPane.setTabMaxWidth(100), tabPane.setTabMaxHeight(100) methods set the maximum and minimum height and width to the tab pane.
Conclusion
TabPane in JavaFX is used to add tabs for adding content to the tabs. Tabs are used for differentiate one content to the other content by keeping the content in different tabs.
Recommended Article
This is a guide to JavaFX TabPane. Here we discuss what can we do with TabPane in JavaFX and how to Create ScrollPane along with Code Implementation and Output. you can also go through our suggested articles to learn more –