An example of JToolBar : Toolbar « Swing JFC « Java

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 Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » ToolbarScreenshots 
An example of JToolBar
An example of JToolBar
   
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// ToolBarExample.java
// An example of JToolBar. The actions used to build the toolbar are also
// placed in a JMenu to further demonstrate the flexibility of the Action
// class. (See the examples in Chapter 3 for more details on Action.)
//

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;

public class ToolBarExample extends JPanel {

  public JTextPane pane;

  public JMenuBar menuBar;

  public JToolBar toolBar;

  String fonts[] "Serif""SansSerif""Monospaced""Dialog",
      "DialogInput" };

  public ToolBarExample() {
    menuBar = new JMenuBar();

    // Create a set of actions to use in both the menu and toolbar
    DemoAction leftJustifyAction = new DemoAction("Left"new ImageIcon(
        "1.gif")"Left justify text"'L');
    DemoAction rightJustifyAction = new DemoAction("Right"new ImageIcon(
        "2.gif")"Right justify text"'R');
    DemoAction centerJustifyAction = new DemoAction("Center",
        new ImageIcon("3.gif")"Center justify text"'M');
    DemoAction fullJustifyAction = new DemoAction("Full"new ImageIcon(
        "4.gif")"Full justify text"'F');

    JMenu formatMenu = new JMenu("Justify");
    formatMenu.add(leftJustifyAction);
    formatMenu.add(rightJustifyAction);
    formatMenu.add(centerJustifyAction);
    formatMenu.add(fullJustifyAction);

    menuBar.add(formatMenu);

    toolBar = new JToolBar("Formatting");
    toolBar.add(leftJustifyAction);
    toolBar.add(rightJustifyAction);
    toolBar.add(centerJustifyAction);
    toolBar.add(fullJustifyAction);

    toolBar.addSeparator();
    JLabel label = new JLabel("Font");
    toolBar.add(label);

    toolBar.addSeparator();
    JComboBox combo = new JComboBox(fonts);
    combo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          pane.getStyledDocument().insertString(
              0,
              "Font ["
                  ((JComboBoxe.getSource())
                      .getSelectedItem() "] chosen!\n",
              null);
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    });
    toolBar.add(combo);

    //  Disable one of the Actions
    fullJustifyAction.setEnabled(false);
  }

  public static void main(String s[]) {

    ToolBarExample example = new ToolBarExample();
    example.pane = new JTextPane();
    example.pane.setPreferredSize(new Dimension(250250));
    example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
    example.toolBar.setMaximumSize(example.toolBar.getSize());

    JFrame frame = new JFrame("Menu Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(example.menuBar);
    frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
    frame.getContentPane().add(example.pane, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }

  class DemoAction extends AbstractAction {

    public DemoAction(String text, Icon icon, String description,
        char accelerator) {
      super(text, icon);
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator,
          Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
      putValue(SHORT_DESCRIPTION, description);
    }

    public void actionPerformed(ActionEvent e) {
      try {
        pane.getStyledDocument().insertString(0,
            "Action [" + getValue(NAME"] performed!\n"null);
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
}

           
         
    
    
  
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. Toolbar Sample
5. JToolBar DemoJToolBar Demo
6. Swing ToolBar DemoSwing ToolBar Demo
7. ToolBar Demo 2ToolBar Demo 2
8. Toolbar and menubarToolbar and menubar
9. Simple toolbarSimple toolbar
10. Working with a ToolbarWorking with a Toolbar
11. ToolBar TestToolBar Test
12. Get ToolBar PropertiesGet ToolBar Properties
13. If the toolbar is to be floatable, it must be added to a container with a BorderLayout.
14. Highlighting Buttons in a JToolbar Container While Under the Cursor
15. JToolbar: Toolbars provide a quick access to the most frequently used commands.JToolbar: Toolbars provide a quick access to the most frequently used commands.
16. Preventing a JToolbar Container from Floating
17. Determining When a Floatable JToolBar Container Changes Orientation
18. Create a vertical toolbar
19. Add various buttons to the toolbar
20. ToolBar UI SampleToolBar UI Sample
21. ToolBar button
22. This class represents a separator for the toolbar buttons.
23. Buttons used in toolbars.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.