  // Swing JToolBarExample - Derek Molloy

  import javax.swing.*;
  import java.awt.*; 
  import java.awt.event.*;
  import javax.swing.border.*;
  import javax.swing.event.*;

  public class JToolBarExample extends JFrame implements ActionListener
  {	
	JToolBar theToolbar;
	JTextArea theTextArea;
	JButton testButton;
	JComboBox comboBox;
	JToggleButton toggleButton;
	

	public JToolBarExample()
	{
		super("JToolBar Example");

		theToolbar = new JToolBar("My Toolbar");
		theToolbar.setFloatable(true);

		theTextArea = new JTextArea(20,20);
		JScrollPane p = new JScrollPane(this.theTextArea,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		Image theImage = this.getToolkit().getImage("next.gif");
		ImageIcon icon = new ImageIcon(theImage);
		JLabel theLabel = new JLabel(icon);
		theToolbar.add(theLabel);

		testButton = new JButton("Test");
		testButton.addActionListener(this);
		theToolbar.add(testButton);
		
		String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
	  	comboBox = new JComboBox(items);
	  	comboBox.addActionListener(this);
	  	theToolbar.add(comboBox);

		theToolbar.addSeparator();
		
		toggleButton = new JToggleButton("Test Toggle");
		toggleButton.addActionListener(this);
		theToolbar.add(toggleButton);
		
		JMenuBar menuBar = new JMenuBar();
		JMenu firstMenu = new JMenu("First Menu");
		JMenuItem item1 = new JMenuItem("First Item", KeyEvent.VK_F);
		JMenuItem item2 = new JMenuItem("Second Item", KeyEvent.VK_S);
		JMenuItem item3 = new JMenuItem("Third Item", KeyEvent.VK_T);
		JMenuItem item4 = new JMenuItem("Some Item", icon);
		JMenuItem sub1 = new JMenuItem("Sub Menu Item 1");
		JMenuItem sub2 = new JMenuItem("Sub Menu Item 2");
		JMenu aSubMenu = new JMenu("Sub Menu");
		aSubMenu.add(sub1);
		aSubMenu.add(sub2);		

		item1.addActionListener(this);
		item2.addActionListener(this);
		item3.addActionListener(this);
		item4.addActionListener(this);
		sub1.addActionListener(this);
		sub2.addActionListener(this);

		menuBar.add(firstMenu);
		firstMenu.add(item1);
		firstMenu.add(item2);
		firstMenu.add(item3);
		firstMenu.add(item4);
		firstMenu.add(aSubMenu);


		this.getContentPane().add("North", theToolbar);
		this.getContentPane().add("Center", p); 	 
	
		this.setJMenuBar(menuBar);
		this.pack();	// set the size automatically
		this.show();	// display the frame
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource().equals(testButton))
			this.theTextArea.append("Test Button Pressed! \n");
		else if (e.getSource().equals(comboBox))
			this.theTextArea.append("Selected Item: " + comboBox.getSelectedItem() + "\n");
		else if (e.getSource().equals(toggleButton))
			this.theTextArea.append("Toggle Item: " + toggleButton.isSelected() + "\n");
		else // must be a menu item
		{
		  this.theTextArea.append("Menu Item \"" + e.getActionCommand() + "\" selected.\n");
		}
	}

	
	public static void main(String[] args)
	{
		new JToolBarExample();
	}
  }
