In this tutorial, we are going to create a simple Swing JTabbedPane, which allows the user to switch between a group of components by clicking on tabs.

Swing JTabbedPane :

We can see the tabbed pane in windows operating by opening the system properties like below.

Tabbedpane Example

As part this example I am going to create a simple Swing JTabbedPane like above.

Swing JTabbedPane Example :
package com.swing.examples;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class TabbedPane extends JFrame {
    JTabbedPane tabs;
    CoursePanel cource;
    SelectCoursePanel selectCourse;

    TabbedPane() {
        super("Tabbed Pane Example");

        // Setting the JTabbedPane Position and Layout as Wrap
        tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

        cource = new CoursePanel();
        selectCourse = new SelectCoursePanel();
        // Adding user defined pannels to JTabbedPane
        tabs.addTab("Cources", cource);
        tabs.addTab("Select Course", selectCourse);
        // Adding JPanels to JTabbedPane
        tabs.addTab("Listing", new JPanel());
        tabs.addTab("Comment", new JTextArea(10, 40));

        tabs.addTab("Register", new JPanel());
        tabs.addTab("Contact Us", new JPanel());

        tabs.addTab("More..", new JPanel());


/*Creating CoursePanel by extending JPanel*/
class CoursePanel extends JPanel {
    JButton addCourse, clear;

    CoursePanel() {
        addCourse = new JButton("Add Course");
        clear = new JButton("Clear");

        setLayout(new FlowLayout());

/*Creating SelectCoursePanel by extending JPanel*/
class SelectCoursePanel extends JPanel {
    JCheckBox java, swing, hibernate;

    SelectCoursePanel() {
        java = new JCheckBox("Java");
        swing = new JCheckBox("Spring");
        hibernate = new JCheckBox("Hibernate");

        setLayout(new FlowLayout());

class JTabbedPaneDemo {
    public static void main(String args[]) throws Exception {
        TabbedPane frame = new TabbedPane();
        frame.setSize(400, 400);

Output :

JTabbedPane Example


On the above example the tabs wrapped based on the width of the panel. If we don’t want to wrap the tabs, it is possible to make it as scroll by changing the tab layout like below :

tabs = new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);

Output :

JTabbedPane Example Scroll

We can also change the JTabbedPane position by using TabPlacement like below.

tabs = new JTabbedPane(JTabbedPane.LEFT,JTabbedPane.WRAP_TAB_LAYOUT);

Output :

JTabbedPane Example Left

Happy Learning 🙂