Saturday, 24 January 2015

How to create JTabbed panes in java

A tabbed pane is a component that appears as a group of folder in a file cabinet.Each folder has a title.When a user select a folder,its contents become visible.Only one of folder can be selected at a time.Tabbed panes are commonly used for setting configuration options.
Tabbed panes are encapsulated by JTabbedPane class which extends  JComponent we will use a default construtor.Tabs are defined  as follow

void addTab(String str,Component comp)

STEPS FOR CREATING JTABBED PANE:


Create a JTabbedPane object.

call addTab() a tab to pane.(The argument to this method define the title of the tab and the component it contains).
Repeat step 2 for each tab.
Add the tabbed pane to content pane.

CONSTRUCTORS OF JTABBED PANE:

JTabbedPane()

When tabbed pane is created using this constructor it will appear at the top position and we can not change the position of tabbed pane using this constructor. 

JTabbedPane(int)

This constructor is applicable when we want to change the position of  tabbed pane if use 0 tabbed pane will create at a top position,if we enter 1 tabbed pane will create at a bottom position,if we 3 tabbed pane will create at left postion and if we want to create tabbed pane at right position then we should enter 4.


JTabbedPane(int,int)

In this tutorial first int specifies the position of tabbed pane while second int specifies the layout policies.

CODING:

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

class tab extends JFrame {
JButton jb,jb1;
JTextField jtf;
tab()
{
super();
setTitle("Simple J Button");
setSize(250, 300);
setVisible(true);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);    
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("      cities     ", new citiesPanel());
jtp.addTab("      colors     ", new colorsPanel());
jtp.addTab("      flavors     ", new flavorsPanel());
getContentPane().add(jtp);
}
}
class citiesPanel extends JPanel
{
public citiesPanel()
{
JButton b1 = new JButton("new york");
add(b1);
JButton b2 = new JButton("Karachi");
add(b2);
JButton b3 = new JButton("Hong Kong");
add(b3);
}
}
class colorsPanel extends JPanel
{
public colorsPanel()
{
JCheckBox c1 = new JCheckBox("Red");
add(c1);
JCheckBox c2 = new JCheckBox("Blue");
add(c2);
JCheckBox c3 = new JCheckBox("Orange");
add(c3);
}
}
class flavorsPanel extends JPanel
{
public flavorsPanel()
{
JComboBox jb = new JComboBox();
jb.addItem("Vanila");
jb.addItem("Choclate");
jb.addItem("Strawberry");
add(jb);
}

}


class mytabbed
{
public static void main(String args[])
{
tab x= new tab();
}
}

OUTPUT:













2 comments:

  1. Thank you vert much i'm new in java programming that helps me its what i'm looking for

    ReplyDelete
    Replies
    1. Thanks for your valuable comment.............

      Delete