Swing ToolBar Demo : 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 
Swing ToolBar Demo
Swing ToolBar Demo
   
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
/*
 * SwingToolBarDemo.java is a 1.4 application that relies on having the Java Look and
 * Feel Graphics Repository (jlfgr-1_0.jar) in the class path. You can download
 * it from http://developer.java.sun.com/developer/techDocs/hi/repository/. When
 * running SwingToolBarDemo from the command line (rather than Java Web Start) put
 * jlfgr-1_0.jar in the class path using one of the following commands (assuming
 * jlfgr-1_0.jar is in a subdirectory named jars):
 
 * java -cp .;jars/jlfgr-1_0.jar SwingToolBarDemo [Microsoft Windows] java -cp
 * .:jars/jlfgr-1_0.jar SwingToolBarDemo [UNIX]
 
 * If that doesn't work, try putting quotation marks around the path:
 
 * java -cp ".;jars/jlfgr-1_0.jar" SwingToolBarDemo [UNIX shell on Win32]
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class SwingToolBarDemo extends JPanel implements ActionListener {
  protected JTextArea textArea;

  protected String newline = "\n";

  static final private String PREVIOUS = "previous";

  static final private String UP = "up";

  static final private String NEXT = "next";

  public SwingToolBarDemo() {
    super(new BorderLayout());

    //Create the toolbar.
    JToolBar toolBar = new JToolBar("Still draggable");
    addButtons(toolBar);

    //Create the text area used for output. Request
    //enough space for 5 rows and 30 columns.
    textArea = new JTextArea(530);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the main panel.
    setPreferredSize(new Dimension(450130));
    add(toolBar, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
  }

  protected void addButtons(JToolBar toolBar) {
    JButton button = null;

    //first button
    button = makeNavigationButton("Back24", PREVIOUS,
        "Back to previous something-or-other""Previous");
    toolBar.add(button);

    //second button
    button = makeNavigationButton("Up24", UP, "Up to something-or-other",
        "Up");
    toolBar.add(button);

    //third button
    button = makeNavigationButton("Forward24", NEXT,
        "Forward to something-or-other""Next");
    toolBar.add(button);
  }

  protected JButton makeNavigationButton(String imageName,
      String actionCommand, String toolTipText, String altText) {
    //Look for the image.
    String imgLocation = "toolbarButtonGraphics/navigation/" + imageName
        ".gif";
    URL imageURL = SwingToolBarDemo.class.getResource(imgLocation);

    //Create and initialize the button.
    JButton button = new JButton();
    button.setActionCommand(actionCommand);
    button.setToolTipText(toolTipText);
    button.addActionListener(this);

    if (imageURL != null) { //image found
      button.setIcon(new ImageIcon(imageURL, altText));
    else //no image found
      button.setText(altText);
      System.err.println("Resource not found: " + imgLocation);
    }

    return button;
  }

  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    String description = null;

    // Handle each button.
    if (PREVIOUS.equals(cmd)) { //first button clicked
      description = "taken you to the previous <something>.";
    else if (UP.equals(cmd)) { // second button clicked
      description = "taken you up one level to <something>.";
    else if (NEXT.equals(cmd)) { // third button clicked
      description = "taken you to the next <something>.";
    }

    displayResult("If this were a real app, it would have " + description);
  }

  protected void displayResult(String actionDescription) {
    textArea.append(actionDescription + newline);
    textArea.setCaretPosition(textArea.getDocument().getLength());
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SwingToolBarDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    SwingToolBarDemo newContentPane = new SwingToolBarDemo();
    newContentPane.setOpaque(true)//content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}

           
         
    
    
  
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 Sample
6. JToolBar DemoJToolBar 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.