by Dinesh Thakur Category: Swing

Sometimes a situation may arise when there are lots of components that cannot be accommodated in a single screen. In that case, tabbed pane is used to create tabs where each tab contains group of related components. When a particular tab is clicked, the components of that tab are displayed in the forefront. Tabbed pane is very common feature of GUI interfaces. It is commonly used in dialog boxes containing lots of commands. A tabbed pane is an object of JTabbedPane class.

Some of the constructors defined by JTabbedPane class are as follows:

JTabbedPane ()

JTabbedPane(int tabPlacement)

where,

tabPlacement specifies the position of tabs-can have one of the four values: TOP, BOTTOM, LEFT or RIGHT

 

import java.awt.BorderLayout;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
public class JavaExampleTabbedPane extends JFrame
  {
    public JavaExampleTabbedPane()
             {
               super("Example of Tab Pane in Java");
               JTabbedPane TbdPn = new JTabbedPane();
               JLabel LblOne = new JLabel("Panel One",SwingConstants.CENTER);
               JPanel PnlOne = new JPanel();
               TbdPn.addTab("Tab One",null,PnlOne,"First Panel");
               JLabel LblTwo = new JLabel("Panel Two",SwingConstants.CENTER);
               JPanel PnlTwo = new JPanel();
               PnlTwo.setBackground(Color.GREEN);
               PnlTwo.add(LblTwo);
               TbdPn.addTab("Tab Two",null,PnlTwo,"Second Panel");
               JLabel LblThree = new JLabel("Panel Three");
               JPanel PnlThree = new JPanel();
               PnlThree.setLayout(new BorderLayout());
               PnlThree.add(new JButton("North"),BorderLayout.NORTH);
               PnlThree.add(new JButton("West"),BorderLayout.WEST);
               PnlThree.add(new JButton("East"),BorderLayout.EAST);
               PnlThree.add(new JButton("South"),BorderLayout.SOUTH);
               PnlThree.add(LblThree,BorderLayout.CENTER);
               TbdPn.addTab("Tab Three",null,PnlThree,"Third panel");
               add(TbdPn);
             }
                          public static void main(String aa[])
                            {
                           JavaExampleTabbedPane Frm = new JavaExampleTabbedPane();
                           Frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                           Frm.setSize(300,350);
                           Frm.setVisible(true);
                         } 
 }

JTabbedPane Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article