In Swing, how to use JTabbedPane to create multiple Tabs

Below are the Swing UI Components used in this example
JPanel
JTabbedPane
JButton
JCheckBox
JComboBox

There are three Tabbed Panes, implemented in below classes.
1.CitiesPanel(extends from JPanel)
2.ColorsPanel(extends from JPanel)
3.FlavorsPanel(extends from JPanel)

CitiesPanel has four JButton components with City names.
When any of these Buttons are clicked, corresponding label on JButton, is set to JTextField.
CitiesPanel implements java.awt.event.ActionListener interface, to handle button click event.

JTabbedPane example
JTabbedPane example

ColorsPanel has three JCheckBox components with Color names.
When any of the check box is selected/unselected, name of check box gets displayed on dialog box, using JOptionPane.
ColorsPanel implements java.awt.event.ItemListener interface, to handle check box check/uncheck event.

JTabbedPane example
JTabbedPane example

FlavorsPanel has a JComboBox, with Fruit names.
When any item is selected from the drop down combox box. The selected item text, is set to JTextField.
FlavorsPanel implements java.awt.event.ItemListener interface, to handle combo box item selected event.

JComboBox-example
JComboBox-example

import javax.swing.*;

import java.awt.event.*;

/* 
 * 
 */

public class TabbedPaneDemo extends JApplet{
public void init()
	{
	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 implements ActionListener{
	JTextField jft;
	public CitiesPanel(){
	JButton b1 = new JButton("Bangalore");
	b1.addActionListener(this);
	add(b1);
	JButton b2 = new JButton("Mumbai");
	b2.addActionListener(this);
	add(b2);
	JButton b3 = new JButton("Chennai");
	b3.addActionListener(this);
	add(b3);
	JButton b4 = new JButton("Delhi");
	b4.addActionListener(this);
	add(b4);

	jft = new JTextField(8);

	add(jft);

	}

	public void actionPerformed(ActionEvent ae)
	{
	String str = ae.getActionCommand();
	jft.setText(str);
	}
}

class ColorsPanel extends JPanel implements ItemListener{

	JCheckBox cb1,cb2,cb3;
	public ColorsPanel(){
	cb1 = new JCheckBox("Yellow");
	add(cb1);

	cb2 = new JCheckBox("Grey");
	add(cb2);

	cb3 = new JCheckBox("Blue");
	add(cb3);

	cb1.addItemListener(this);
	cb2.addItemListener(this);
	cb3.addItemListener(this);
	}

	public void itemStateChanged(ItemEvent ie)
	{
	JCheckBox cb = (JCheckBox)ie.getItem();
	JOptionPane.showMessageDialog(null, "You Selected:"+cb.getText());
	}
}


class FlavorsPanel extends JPanel implements ItemListener{
	JTextField jft1;
	public FlavorsPanel(){
	JComboBox jcb = new JComboBox(); //Drop down menu
	jcb.addItem("Mango");
	jcb.addItem("Orange");
	jcb.addItem("Chocolate");
	add(jcb);

	jft1 = new JTextField(8);
	jft1.setText("No Item Selected");
	add(jft1);

	jcb.addItemListener(this);
	}

	public void itemStateChanged(ItemEvent ie)
	{
	String str = (String)ie.getItem();

	jft1.setText(str);
	}
}

You may also like to read:
Advantages of Swing over Applet

Leave a Reply