Java Swing Tutorial - Java Swing JTabbedPane








A JTabbedPane component acts like a container for other Swing components, arranging them in a tabbed pane.

We can display tabs using a title, an icon, or both.

A JTabbedPane can show the tabs at the top, bottom, left, or right.

The getTabCount() method returns the number of tabs in a JTabbedPane.

Every tab inside a JTabbedPane has an index. The first tab has an index of 0, the second tab has an index of 1, and so on.

We can get the component that represents a tab using its index.

JPanel contactsPanel  = tabbedPane.getTabComponentAt(1);




Adding and Removing Tabs

We can use the following methods to add tabs to a JTabbedPane.

public void addTab(String title, Component component)
public void addTab(String title, Icon icon, Component component)
public void addTab(String title, Icon icon, Component component, String tip)
public void insertTab(String title, Icon icon, Component component, String tip, int index)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
//from w  w w . j a v a2 s. c  o  m
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
  static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
      Color.MAGENTA };

  static void add(JTabbedPane tabbedPane, String label) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    button.setBackground(colors[count]);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i]);
    }

    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}




Specifying a tab's location: TOP, BOTTOM, LEFT, or RIGHT

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
//from  w ww  .ja  va  2  s  . c  o  m
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class TabLocation {
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }

  public static void main(String args[]) {

    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }

    tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

Changing background, foreground and icon

We can use the following methods to change background color, enable disable a tab.

public void setBackgroundAt(int index, Color background)
public void setForegroundAt(int index, Color foreground)
public void setEnabledAt(int index, boolean enabled)
public void setDisabledIconAt(int index, Icon disabledIcon)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
/*ww  w. j  a va 2  s.com*/
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class Main {
  static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
      Color.MAGENTA };

  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
      tabbedPane.setBackgroundAt(i,colors[i]);
    }
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

Remove a Tab

To remove a tab, you can remove a specific tab with removeTabAt(int index), remove(int index), or remove(Component component)

Removing all tabs with removeAll()

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
//w  w  w.  j  av a 2s .c o  m
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class Main {

  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }

    tabbedPane.removeAll();
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

Listening for Selected Tab Changes

import java.awt.BorderLayout;
/*from   w  ww  . j a  v  a 2  s. co  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabSample {
  static void add(JTabbedPane tabbedPane, String label) {
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i]);
    }

    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changeEvent) {
        JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
        int index = sourceTabbedPane.getSelectedIndex();
        System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
      }
    };
    tabbedPane.addChangeListener(changeListener);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

TabLayout Policy: SCROLL_TAB_LAYOUT or WRAP_TAP_LAYOUT

import java.awt.BorderLayout;
//w  w w. ja va 2 s . c om
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class TabSampleTabLayoutPolicy {
  static void add(JTabbedPane tabbedPane, String label) {
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i]);
    }

    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

Add Components to a JTabbedPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*from  www . j  a v  a 2s  . co m*/
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class JDK6TabbedPaneExample {

  private JFrame frame = new JFrame();

  private JTabbedPane tabbedPane = new JTabbedPane();

  private JButton addTabButton = new JButton("Add Tab");

  private ImageIcon closeXIcon = new ImageIcon("C:/CloseX.gif");

  private Dimension closeButtonSize;

  private int tabCounter = 0;

  public JDK6TabbedPaneExample() {

    addTabButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        add();
        
      }
      
    });
    closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2);

    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.add(addTabButton, BorderLayout.SOUTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setVisible(true);

  }

  public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);

    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));

    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        int closeTabNumber = tabbedPane.indexOfComponent(content);
        tabbedPane.removeTabAt(closeTabNumber);
      }
    });

    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);

    tabbedPane.addTab(null, content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
  }

  public static void main(String[] args) {
    JDK6TabbedPaneExample main = new JDK6TabbedPaneExample();
  }

}