Simple toolbar : Toolbar « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing JFC » ToolbarScreenshots 
Simple toolbar
Simple toolbar
    

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class SimpleToolbar extends JFrame {

  public static final String FontNames[] "Serif""SansSerif""Courier" };

  protected Font fonts[];

  protected JFileChooser fileChooser = new JFileChooser();

  protected JToolBar toolBar;

  protected JComboBox cbFonts;

  protected SmallToggleButton bBold;

  protected SmallToggleButton bItalic;

  public SimpleToolbar() {
    super();
    setSize(450350);

    fonts = new Font[FontNames.length];
    for (int k = 0; k < FontNames.length; k++)
      fonts[knew Font(FontNames[k], Font.PLAIN, 12);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    createToolBar();
    setVisible(true);
  }

  protected void createToolBar() {
    ImageIcon iconNew = new ImageIcon("file_new.gif");
    Action actionNew = new AbstractAction("New", iconNew) {
      public void actionPerformed(ActionEvent e) {
        ;
      }
    };
    ImageIcon iconOpen = new ImageIcon("file_open.gif");
    Action actionOpen = new AbstractAction("Open...", iconOpen) {
      public void actionPerformed(ActionEvent e) {
      }
    };
    ImageIcon iconSave = new ImageIcon("file_save.gif");
    Action actionSave = new AbstractAction("Save...", iconSave) {
      public void actionPerformed(ActionEvent e) {
      }
    };
    Action actionExit = new AbstractAction("Exit") {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    };
    toolBar = new JToolBar();
    JButton bNew = new SmallButton(actionNew, "New text");
    toolBar.add(bNew);

    JButton bOpen = new SmallButton(actionOpen, "Open text file");
    toolBar.add(bOpen);

    JButton bSave = new SmallButton(actionSave, "Save text file");
    toolBar.add(bSave);

    ActionListener fontListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      }
    };

    toolBar.addSeparator();
    cbFonts = new JComboBox(FontNames);
    cbFonts.setMaximumSize(cbFonts.getPreferredSize());
    cbFonts.setToolTipText("Available fonts");
    ActionListener lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int index = cbFonts.getSelectedIndex();
        if (index < 0)
          return;
      }
    };
    cbFonts.addActionListener(lst);
    toolBar.add(cbFonts);

    toolBar.addSeparator();

    ImageIcon img1 = new ImageIcon("font_bold1.gif");
    ImageIcon img2 = new ImageIcon("font_bold2.gif");
    bBold = new SmallToggleButton(false, img1, img2, "Bold font");
    lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      }
    };
    bBold.addActionListener(lst);
    toolBar.add(bBold);

    img1 = new ImageIcon("font_italic1.gif");
    img2 = new ImageIcon("font_italic2.gif");
    bItalic = new SmallToggleButton(false, img1, img2, "Italic font");
    lst = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      }
    };
    bItalic.addActionListener(lst);
    toolBar.add(bItalic);

    getContentPane().add(toolBar, BorderLayout.NORTH);

  }
  public static void main(String[] a){
    new SimpleToolbar();
      
    }

}
class SmallButton extends JButton implements MouseListener {
  protected Border m_raised;

  protected Border m_lowered;

  protected Border m_inactive;

  public SmallButton(Action act, String tip) {
    super((Iconact.getValue(Action.SMALL_ICON));
    m_raised = new BevelBorder(BevelBorder.RAISED);
    m_lowered = new BevelBorder(BevelBorder.LOWERED);
    m_inactive = new EmptyBorder(2222);
    setBorder(m_inactive);
    setMargin(new Insets(1111));
    setToolTipText(tip);
    addActionListener(act);
    addMouseListener(this);
    setRequestFocusEnabled(false);
  }

  public float getAlignmentY() {
    return 0.5f;
  }

  public void mousePressed(MouseEvent e) {
    setBorder(m_lowered);
  }

  public void mouseReleased(MouseEvent e) {
    setBorder(m_inactive);
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
    setBorder(m_raised);
  }

  public void mouseExited(MouseEvent e) {
    setBorder(m_inactive);
  }
}

class SmallToggleButton extends JToggleButton implements ItemListener {
  protected Border raised;

  protected Border lowered;

  public SmallToggleButton(boolean selected, ImageIcon imgUnselected,
      ImageIcon imgSelected, String tip) {
    super(imgUnselected, selected);
    setHorizontalAlignment(CENTER);
    setBorderPainted(true);
    raised = new BevelBorder(BevelBorder.RAISED);
    lowered = new BevelBorder(BevelBorder.LOWERED);
    setBorder(selected ? lowered : raised);
    setMargin(new Insets(1111));
    setToolTipText(tip);
    setRequestFocusEnabled(false);
    setSelectedIcon(imgSelected);
    addItemListener(this);
  }

  public float getAlignmentY() {
    return 0.5f;
  }

  public void itemStateChanged(ItemEvent e) {
    setBorder(isSelected() ? lowered : raised);
  }
}


           
         
    
    
    
  
Related examples in the same category
1.Create two toolbars
2.Shows a vertical toolbar.
3.A simple frame containing a toolbar made up of several ButtonsA simple frame containing a toolbar made up of several Buttons
4.An example of JToolBarAn example of JToolBar
5.Toolbar SampleToolbar Sample
6.JToolBar DemoJToolBar Demo
7.Swing ToolBar DemoSwing ToolBar Demo
8.ToolBar Demo 2ToolBar Demo 2
9.Toolbar and menubarToolbar and menubar
10.Working with a ToolbarWorking with a Toolbar
11.Get ToolBar PropertiesGet ToolBar Properties
12.If the toolbar is to be floatable, it must be added to a container with a BorderLayout.
13.Highlighting Buttons in a JToolbar Container While Under the Cursor
14.JToolbar: Toolbars provide a quick access to the most frequently used commands.JToolbar: Toolbars provide a quick access to the most frequently used commands.
15.Preventing a JToolbar Container from Floating
16.Determining When a Floatable JToolBar Container Changes Orientation
17.Create a vertical toolbar
18.Add various buttons to the toolbar
19.ToolBar UI SampleToolBar UI Sample
20.ToolBar button
21.This class represents a separator for the toolbar buttons.
22.Buttons used in toolbars.
23.A frame with a toolbar and menu for color changesA frame with a toolbar and menu for color changes
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.