TabPane
A TabPane is a container that holds multiple Tab objects, letting the user switch between different content panels, like browser tabs.
- Each
Tabhas a title and content. - Tabs can be closable or fixed.
Basic Example
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 TabPaneExample extends Application {
@Override
public void start(Stage stage) {
// Create TabPane
TabPane tabPane = new TabPane();
// First Tab
Tab tab1 = new Tab("Home");
tab1.setContent(new Label("This is the Home tab"));
tab1.setClosable(false); // cannot close
// Second Tab
Tab tab2 = new Tab("Settings");
VBox settingsBox = new VBox(new Label("Settings content here"));
tab2.setContent(settingsBox);
// Third Tab
Tab tab3 = new Tab("About");
tab3.setContent(new Label("About this app"));
// Add tabs to TabPane
tabPane.getTabs().addAll(tab1, tab2, tab3);
Scene scene = new Scene(tabPane, 400, 200);
stage.setTitle("TabPane Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Result:
- Tabs appear horizontally at the top by default.
- Clicking a tab shows its content.
Key Properties and Methods
tab.setText("Title"); // set tab title
tab.setContent(Node); // set content inside the tab
tab.setClosable(true/false); // allow user to close the tab
tabPane.getTabs(); // list of tabs
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); // disable closing for all tabs
TabClosingPolicy options:
ALL_TABS→ all tabs can be closedSELECTED_TAB→ only selected tab can be closedUNAVAILABLE→ no tab can be closed
FXML Version
<TabPane xmlns:fx="http://javafx.com/fxml" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Home">
<content>
<Label text="This is the Home tab"/>
</content>
</Tab>
<Tab text="Settings">
<content>
<VBox>
<Label text="Settings content here"/>
</VBox>
</content>
</Tab>
<Tab text="About">
<content>
<Label text="About this app"/>
</content>
</Tab>
</tabs>
</TabPane>
Use Cases
- Tabbed panels in settings or dashboards
- Browser-like interface
- Multiple document views
- Forms split into sections