JTabbedPane Example Java Swing

JTabbedPane: Tabbed pane is a common user interface component that provide an easy access to more than one panel(like CardLayout manager ). Each tab is associated with a single component that will be displayed when the tab is selected.

In the following JTabbedPane program, three tabs are added to the tabbed pane. 3 classes are made for each tab. Each class is executed and displayed when a tab is clicked.

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

public class JTPDemo extends JApplet  
{
  public void init()   
  {
    JTabbedPane jt = new JTabbedPane();

    jt.add("Colors", new CPanel());
    jt.add( "Fruits", new FPanel());
    jt.add("Vitamins", new VPanel( ) ) ;

    getContentPane().add(jt);
  }
}
class CPanel extends JPanel   
{
  public CPanel()   
  {
    JCheckBox cb1 = new JCheckBox("Red");
    JCheckBox cb2 = new JCheckBox("Green");
    JCheckBox cb3 = new JCheckBox("Blue");
    add(cb1);    add(cb2);    add(cb3) ;
  }      
}
class FPanel extends JPanel   
{
  public FPanel()   
  {
    JComboBox cb = new JComboBox();
    cb.addItem("Apple");
    cb.addItem("Mango");
    cb.addItem("Pineapple");
    add(cb);
  }
}
class VPanel extends JPanel   
{
  public VPanel()   
  {
    JButton b1 = new JButton("Vit-A");
    JButton b2 = new JButton("Vit-B");
    JButton b3 = new JButton("Vit-C");
    add(b1);    add(b2);    add(b3);
  }     
}

Following is the applet code to run the above JApplet:



jt.add(“Colors”, new CPanel());

"Colors" is displayed as a tab and when clicked class CPanel is executed.