TabPane class is a part of JavaFX. TabPane allows switching between several tabs. TabPane acts as a container of tabs. The positions of the tabs can be specified using the setSide() function. when the tabs do not fit in the TabPane, a menu button appears at the upper right corner of TabPane which displays all the tabs of the Tabpane.
Constructors of the class:
- TabPane(): Creates a new TabPane with no tabs.
- TabPane(Tab… t): Creates a new TabPane with specified tabs.
Commonly Used Methods:
Method | Explanation |
---|---|
getSide() | Returns the current position of tabs on the TabPane |
getTabs() | Returns the tabs of the TabPane. |
setSide(Side v) | The position to place the tabs in this TabPane. |
setSelectionModel(SingleSelectionModel v) | Sets the model used for tab selection. |
getSelectionModel() | Returns the selection model for tab selection. |
getTabMaxHeight() | Returns the max height of the tab in TabPane. |
getTabMinHeight() | Returns the min height of the tab in TabPane. |
getTabMaxWidth() | Returns the max Width of the tab in TabPane. |
getTabMinWidth() | Returns the min Width of the tab in TabPane. |
setTabMaxHeight(double v) | Sets the max height of the tab in TabPane. |
setTabMinHeight(double v) | Sets the min height of the tab in TabPane. |
setTabMaxWidth(double v) | Sets the max width of the tab in TabPane. |
setTabMinWidth(double v) | Sets the min width of the tab in TabPane. |
Below programs illustrate the use of TabPane class:
- Java program to create multiple tabs and add it to the TabPane: In this program we will create a Tabpane named tabpane. To add multiple tabs we will use a for loop and then add tabs to the tabpane. Create a Tab named tab. 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. Now create a TabPane named tabpane and add the tab to the tabpane. After that 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 multiple tabs
// 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
TabPane_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"Creating Tab"
);
// create a tabpane
TabPane tabpane =
new
TabPane();
// create multiple tabs
for
(
int
i =
0
; i <
10
; i++) {
// create Tab
Tab tab =
new
Tab(
"Tab_"
+ (
int
)(i +
1
));
// create a label
Label label =
new
Label(
"This is Tab: "
+ (
int
)(i +
1
));
// add label to the tab
tab.setContent(label);
// add tab
tabpane.getTabs().add(tab);
}
// 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:
- Java program to create multiple tabs and add it to the TabPane and also create a tab which on selected will create new tabs: In this program we will create a Tabpane named tabpane. We will add multiple tabs to the tabpane. To add multiple tabs we will use a for loop. We will create a Tab named tab. Now create a Label named label. We will add a label to the tab by using the function setContent(). We will also create a tab named newtab. When it is selected then it will create a new tab. Add event handler to the tab by using setOnSelectionChanged() function. The event handler will create a new tab and add it before the new tab in tabpane using getTabs().add() function and select the last tab using the getSelectionModel().select() function. The title of the tab will be passed as arguments. We will create a TabPane named tabpane and add the tab to the tabpane and add the tabpane to the scene and scene to the stage. Display the stage using the show() function.
// Java program to create multiple tabs and
// add it to the tabPane and also create a
// tab which on selected will create new tabs
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
javafx.event.Event;
import
javafx.event.EventHandler;
public
class
TabPane_2
extends
Application {
// counter of tabs
int
counter =
0
;
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"Creating Tab"
);
// create a tabpane
TabPane tabpane =
new
TabPane();
for
(
int
i =
0
; i <
5
; i++) {
// create Tab
Tab tab =
new
Tab(
"Tab_"
+ (
int
)(counter +
1
));
// create a label
Label label =
new
Label(
"This is Tab: "
+ (
int
)(counter +
1
));
counter++;
// add label to the tab
tab.setContent(label);
// add tab
tabpane.getTabs().add(tab);
}
// create a tab which
// when pressed creates a new tab
Tab newtab =
new
Tab();
// action event
EventHandler<Event> event =
new
EventHandler<Event>() {
public
void
handle(Event e)
{
if
(newtab.isSelected())
{
// create Tab
Tab tab =
new
Tab(
"Tab_"
+ (
int
)(counter +
1
));
// create a label
Label label =
new
Label(
"This is Tab: "
+ (
int
)(counter +
1
));
counter++;
// add label to the tab
tab.setContent(label);
// add tab
tabpane.getTabs().add(
tabpane.getTabs().size() -
1
, tab);
// select the last tab
tabpane.getSelectionModel().select(
tabpane.getTabs().size() -
2
);
}
}
};
// set event handler to the tab
newtab.setOnSelectionChanged(event);
// add newtab
tabpane.getTabs().add(newtab);
// 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:
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/TabPane.html