java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a simple JTabbedPane?

JTabbedPane allows some components such as panels to share the same view. By selecting the corresponding tab the panel will be displayed on the user's screen. To add tabs to the JTabbedPane class you can use the addTab() method.

To the addTab() method pass the title of the tab and the component you wish to display as the content of the tab. In the snippet below we just pass a simple JPanel as the content of the tab. You can also define an image icon for the tab which makes the program looks better.

Here is a simple example creating JTabbedPane containing three tabs, the Dashboard, Transactions and Account tab.

package com.javacoderanch.example.swing;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.BorderLayout;

public class TabbedPaneDemo extends JPanel {
    public TabbedPaneDemo() {

    private void initializeUI() {
        JTabbedPane pane = new JTabbedPane();

        JPanel dashboardPanel = new JPanel();
        dashboardPanel.add(new JLabel("Dashboard"));
        // Add Dashboard Tab
        pane.addTab("Dashboard", dashboardPanel);

        JPanel transactionPanel = new JPanel();
        transactionPanel.add(new JLabel("Transactions"));
        // Add Transactions Tab
        pane.addTab("Transactions", transactionPanel);

        JPanel accountPanel = new JPanel();
        accountPanel.add(new JLabel("Account"));
        // Add Account Tab
        pane.addTab("Account", accountPanel);

        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));
        this.add(pane, BorderLayout.CENTER);

    public static void showFrame() {
        JPanel panel = new TabbedPaneDemo();

        JFrame frame = new JFrame("JTabbedPane Demo");

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {