TabPane in JavaFX is a useful feature that lets you display various pieces of information in a single window. It is comparable to the Accordion in JavaFX, which you might want to explore to expand your knowledge.
How to use the TabPane in JavaFX
TabPane is really easy to make in JavaFX. There is also another way to make the TabPane easily and that is using the Scene Builder. Scene Builder is used to making graphical user interfaces in JavaFX. It provides a drag-and-drop interface for designing the user interface of a JavaFX application, allowing developers to easily create UI elements such as buttons, labels, and text fields, and arrange them in a visually appealing way.
TabPane is a container component that allows you to organize content into tabbed sections. It allows the user to switch between different tabs, each of which contains a separate piece of content. TabPane in JavaFX provides a convenient way to organize content into multiple tabs, allowing the user to easily switch between different pieces of content.
This tutorial will guide you through the process of creating and utilizing the TabPane feature in JavaFX. I will demonstrate the creation of the JavaFX TabPane and provide additional examples for your reference.
Create a TabPane
In this example, we will use a code to create the JavaFX TabPane instead of using the Scene Builder. Coding is the best practice when you are a beginner at something new. We need to use the TabPane class in JavaFX to create the TabPane. The following code will show you how to do it.
TabPane tabPane = new TabPane();
This line of code will make an empty TabPane and making Tabs is also easy, you just need to make an object of the Tab class in JavaFX to make tabs for your JavaFX TabPane.
Create a Tab
The tab consists of a header and a content panel. The header typically contains a label that identifies the content in the panel. The content panel contains the actual content that is displayed when the user selects the corresponding tab.
JavaFX provides a class called
Tab to create tabs. The Tab class is a subclass of the
javafx.scene.control.Tab class, and it contains properties for setting the text title and the content of the tab. The content of the tab can be any JavaFX Node, such as a Pane, GridPane, VBox, HBox, or any other layout container. The following example code below will show you how JavaFX Tab is created.
Tab tab1 = new Tab(); tab1.setText("Personal Information"); tab1.setContent(new Button("This is a button")); tab1.setClosable(false); Tab tab2 = new Tab(); tab2.setText("Address"); tab2.setContent(new Button("A button in tab 2")); tab2.setClosable(false); Tab tab3 = new Tab(); tab3.setText("Other Information"); tab3.setContent(new Label("This is a label inside the tab 3")); tab3.setClosable(false);
Display the Tab in TabPane
To display a Tab in a TabPane, you can add the Tab to the TabPane using the getTabs() method and the add() method. If you have multiple tabs, you can use the addAll() method. Here’s an example code of how to add tabs to the JavaFX TabPane.
tabPane.getTabs().addAll(tab1, tab2, tab3);
Showing the TabPane in window
In this example, we will show the JavaFX TabPane to our window using the StackPane in JavaFX based on the examples above. The example below will consist of creating the object of layout in JavaFX, Scene, and Stage. We will also use the StackPane’s getChildren() method to add the TabPane. Please proceed to the example below to learn more about how to show the TabPane in a Window of JavaFX application.
// This code should be placed inside the start() method. TabPane tabPane = new TabPane(); Tab tab1 = new Tab(); tab1.setText("Personal Information"); tab1.setContent(new Button("This is a button")); tab1.setClosable(false); Tab tab2 = new Tab(); tab2.setText("Address"); tab2.setContent(new Button("A button in tab 2")); tab2.setClosable(false); Tab tab3 = new Tab(); tab3.setText("Other Information"); tab3.setContent(new Label("This is a label inside the tab 3")); tab3.setClosable(false); tabPane.getTabs().addAll(tab1, tab2, tab3); StackPane layout = new StackPane(); layout.getChildren().add(tabPane); Scene scene = new Scene(layout, 500, 350); stage.setScene(scene); stage.setTitle("TabPane in JavaFX"); stage.show();
Leave a Reply