Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tab Control in Java ( in AWT)

0.00/5 (No votes)
7 Jun 2010 1  
This is a Java tab Control like SS Tab in VB. Its Cool One

Sample Image - tabcontrol.gif

Introduction

Dear friends, this is one of most useful components in the Java programming world. Here, I am using the Card Layout for developing this component. Download the project files and run the run.bat file to get the output.

Explanation

The Following files are used in the Tab Control.

  • Border.java, BorderPanel.java, Scrollbar.java are used to create a customized scroll bar.
  • Scrollpanel.java is used to embed a customized scroll bar into a Panel.
  • TabPanel.java is used to create tabs.
  • chatClient1.java is the main applet

Source Code

Here is the source code for setting a Tab Panel:

import java.awt.Panel;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;

public class TapPanel extends Panel
{
    TabPanel(chatClient1 app)
    {
        // Initialize the Components
        chatclient = app;	
        // Main Panel
        RightPanel = new CPanel(300, 500);

        Panel panel2 = new Panel();
        CardLayout cardlayout = new CardLayout();
        panel2.setLayout(cardlayout);
        UserPanel = new Panel();	  
        UserPanel.add("Center",new Label("User"));

        ImgScrollPanel = new Panel();
        ImgScrollPanel.setLayout(new BorderLayout());

        BuddyPanel = new Panel();
        BuddyPanel.setLayout(new BorderLayout());

        RoomPanel = new Panel();
        RoomPanel.setLayout(new BorderLayout());
	  
        panel2.add("UserPanel", UserPanel);
        panel2.add("RoomPanel", RoomPanel); 
        panel2.add("ImgScrollPanel", ImgScrollPanel); 
        panel2.add("BuddyPanel", BuddyPanel);

        cardlayout.show(panel2, "UserPanel");

        RightTab = new BorderPanel(this,chatclient, cardlayout, panel2, 210, 270);

        RightTab.addTab("USERS", "UserPanel");
        RightTab.addTab("ROOMS", "RoomPanel"); 
        RightTab.addTab("IMAGES", "ImgScrollPanel");
        RightTab.addTab("BUDDY", "BuddyPanel");
        RightPanel.add(RightTab);
        add(RightPanel);  
    }

    Panel LeftPanel, BuddyPanel, RoomPanel, ImgScrollPanel, UserPanel;
    CPanel RightPanel;
    BorderPanel RightTab;
    chatClient1 chatclient;
    public int i;
}

Finally,

If you want a TabPanel, you can import the TabPanel class in your applet or main application. Adding the code tab = new TabPanel(this); will embed it into your application.

Jeeva S

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here