Java JTabbedPane

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JTabbedPane

The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon. It inherits JComponent class.

JTabbedPane class declaration

Let's see the declaration for javax.swing.JTabbedPane class.

  1. public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants  

Commonly used Constructors:




Creates an empty TabbedPane with a default tab placement of JTabbedPane.Top.

JTabbedPane(int tabPlacement)

Creates an empty TabbedPane with a specified tab placement.

JTabbedPane(int tabPlacement, int tabLayoutPolicy)

Creates an empty TabbedPane with a specified tab placement and tab layout policy.

Java JTabbedPane Example

  1. import javax.swing.*;  
  2. public class TabbedPaneExample {  
  3. JFrame f;  
  4. TabbedPaneExample(){  
  5.     f=new JFrame();  
  6.     JTextArea ta=new JTextArea(200,200);  
  7.     JPanel p1=new JPanel();  
  8.     p1.add(ta);  
  9.     JPanel p2=new JPanel();  
  10.     JPanel p3=new JPanel();  
  11.     JTabbedPane tp=new JTabbedPane();  
  12.     tp.setBounds(50,50,200,200);  
  13.     tp.add("main",p1);  
  14.     tp.add("visit",p2);  
  15.     tp.add("help",p3);    
  16.     f.add(tp);  
  17.     f.setSize(400,400);  
  18.     f.setLayout(null);  
  19.     f.setVisible(true);  
  20. }  
  21. public static void main(String[] args) {  
  22.     new TabbedPaneExample();  
  23. }}  


JAVA Jtabbedpane 1