Applet Menu Bar Demo : Applet « 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 » AppletScreenshots 
Applet Menu Bar Demo
  

/*
 * Copyright (c) 2000 David Flanagan. All rights reserved. This code is from the
 * book Java Examples in a Nutshell, 2nd Edition. It is provided AS-IS, WITHOUT
 * ANY WARRANTY either expressed or implied. You may study, use, and modify it
 * for any non-commercial purpose. You may distribute it non-commercially as
 * long as you retain this notice. For a commercial use license, or to purchase
 * the book (recommended), visit http://www.davidflanagan.com/javaexamples2.
 */

import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class AppletMenuBarDemo extends Applet {
  public void init() {
    AppletMenuBar menubar = new AppletMenuBar();
    menubar.setForeground(Color.black);
    menubar.setHighlightColor(Color.red);
    menubar.setFont(new Font("helvetica", Font.BOLD, 12));
    this.setLayout(new BorderLayout());
    this.add(menubar, BorderLayout.NORTH);

    PopupMenu file = new PopupMenu();
    file.add("New...");
    file.add("Open...");
    file.add("Save As...");
    PopupMenu edit = new PopupMenu();
    edit.add("Cut");
    edit.add("Copy");
    edit.add("Paste");

    menubar.addMenu("File", file);
    menubar.addMenu("Edit", edit);
  }
}

/*
 * Copyright (c) 2000 David Flanagan. All rights reserved. This code is from the
 * book Java Examples in a Nutshell, 2nd Edition. It is provided AS-IS, WITHOUT
 * ANY WARRANTY either expressed or implied. You may study, use, and modify it
 * for any non-commercial purpose. You may distribute it non-commercially as
 * long as you retain this notice. For a commercial use license, or to purchase
 * the book (recommended), visit http://www.davidflanagan.com/javaexamples2.
 */

class AppletMenuBar extends Panel {
  // Menubar contents
  Vector labels = new Vector();

  Vector menus = new Vector();

  // Properties
  Insets margins = new Insets(310310)// top, left, bottom, right

  int spacing = 10// Space between menu labels

  Color highlightColor; // Rollover color for labels

  // internal stuff
  boolean remeasure = true// Whether the labels need to be remeasured

  int[] widths; // The width of each label

  int[] startPositions; // Where each label starts

  int ascent, descent; // Font metrics

  Dimension prefsize = new Dimension()// How big do we want to be?

  int highlightedItem = -1// Which item is the mouse over?

  /**
   * Create a new component that simulates a menubar by displaying the
   * specified labels. Whenever the user clicks the specified label, popup up
   * the PopupMenu specified in the menus array. Elements of the menus arra
   * may be a static PopupMenu object, or a PopupMenuFactory object for
   * dynamically creating menus. Perhaps we'll also provide some other kind of
   * constructor or factory method that reads popup menus out of a config
   * file.
   */
  public AppletMenuBar() {
    // We'd like these kinds of events to be delivered
    enableEvents(AWTEvent.MOUSE_EVENT_MASK
        | AWTEvent.MOUSE_MOTION_EVENT_MASK);
  }

  /** Add a popup menu to the menubar */
  public void addMenu(String label, PopupMenu menu) {
    insertMenu(label, menu, -1);
  }

  /** Insert a popup menu into the menubar */
  public void insertMenu(String label, PopupMenu menu, int index) {
    if (index < 0)
      index += labels.size() 1// Position to put it at
    this.add(menu)// Popup belongs to us
    labels.insertElementAt(label, index)// Remember the label
    menus.insertElementAt(menu, index)// Remember the menu
    remeasure = true// Remeasure everything
    invalidate()// Container must relayout
  }

  /** Property accessor methods for margins property */
  public Insets getMargins() {
    return (Insetsmargins.clone();
  }

  public void setMargins(Insets margins) {
    this.margins = margins;
    remeasure = true;
    invalidate();
  }

  /** Property accessor methods for spacing property */
  public int getSpacing() {
    return spacing;
  }

  public void setSpacing(int spacing) {
    if (this.spacing != spacing) {
      this.spacing = spacing;
      remeasure = true;
      invalidate();
    }
  }

  /** Accessor methods for highlightColor property */
  public Color getHighlightColor() {
    if (highlightColor == null)
      return getForeground();
    else
      return highlightColor;
  }

  public void setHighlightColor(Color c) {
    if (highlightColor != c) {
      highlightColor = c;
      repaint();
    }
  }

  /** We override the setFont() method so we can remeasure */
  public void setFont(Font f) {
    super.setFont(f);
    remeasure = true;
    invalidate();
  }

  /** Override these color property setter method so we can repaint */
  public void setForeground(Color c) {
    super.setForeground(c);
    repaint();
  }

  public void setBackground(Color c) {
    super.setBackground(c);
    repaint();
  }

  /**
   * This method is called to draw tell the component to redraw itself. If we
   * were implementing a Swing component, we'd override paintComponent()
   * instead
   */
  public void paint(Graphics g) {
    if (remeasure)
      measure()// Remeasure everything first, if needed

    // Figure out Y coordinate to draw at
    Dimension size = getSize();
    int baseline = size.height - margins.bottom - descent;
    // Set the font to draw with
    g.setFont(getFont());
    // Loop through the labels
    int nummenus = labels.size();
    for (int i = 0; i < nummenus; i++) {
      // Set the drawing color. Highlight the current item
      if ((i == highlightedItem&& (highlightColor != null))
        g.setColor(getHighlightColor());
      else
        g.setColor(getForeground());

      // Draw the menu label at the position computed in measure()
      g.drawString((Stringlabels.elementAt(i), startPositions[i],
          baseline);
    }

    // Now draw a groove at the bottom of the menubar.
    Color bg = getBackground();
    g.setColor(bg.darker());
    g.drawLine(0, size.height - 2, size.width, size.height - 2);
    g.setColor(bg.brighter());
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
  }

  /** Called when a mouse event happens over the menubar */
  protected void processMouseEvent(MouseEvent e) {
    int type = e.getID()// What type of event?
    int item = findItemAt(e.getX())// Over which menu label?

    if (type == MouseEvent.MOUSE_PRESSED) {
      // If it was a mouse down event, then pop up the menu
      if (item == -1)
        return;
      Dimension size = getSize();
      PopupMenu pm = (PopupMenumenus.elementAt(item);
      if (pm != null)
        pm.show(this, startPositions[item3, size.height);

    else if (type == MouseEvent.MOUSE_EXITED) {
      // If the mouse left the menubar, then unhighlight
      if (highlightedItem != -1) {
        highlightedItem = -1;
        if (highlightColor != null)
          repaint();
      }
    else if ((type == MouseEvent.MOUSE_MOVED)
        || (type == MouseEvent.MOUSE_ENTERED)) {
      // If the mouse moved, change the highlighted item, if necessary
      if (item != highlightedItem) {
        highlightedItem = item;
        if (highlightColor != null)
          repaint();
      }
    }
  }

  /** This method is called when the mouse moves */
  protected void processMouseMotionEvent(MouseEvent e) {
    processMouseEvent(e);
  }

  /** This utility method converts an X coordinate to a menu label index */
  protected int findItemAt(int x) {
    // This could be a more efficient search...
    int nummenus = labels.size();
    int halfspace = spacing / 1;
    int i;
    for (i = nummenus - 1; i >= 0; i--) {
      if ((x >= startPositions[i- halfspace)
          && (x <= startPositions[i+ widths[i+ halfspace))
        break;
    }
    return i;
  }

  /**
   * Measure the menu labels, and figure out their positions, so we can
   * determine when a click happens, and so we can redraw efficiently.
   */
  protected void measure() {
    // Get information about the font
    FontMetrics fm = this.getFontMetrics(getFont());
    // Remember the basic font size
    ascent = fm.getAscent();
    descent = fm.getDescent();
    // Create arrays to hold the measurements and positions
    int nummenus = labels.size();
    widths = new int[nummenus];
    startPositions = new int[nummenus];

    // Measure the label strings and
    // figure out the starting position of each label
    int pos = margins.left;
    for (int i = 0; i < nummenus; i++) {
      startPositions[i= pos;
      String label = (Stringlabels.elementAt(i);
      widths[i= fm.stringWidth(label);
      pos += widths[i+ spacing;
    }

    // Compute our preferred size from this data
    prefsize.width = pos - spacing + margins.right;
    prefsize.height = ascent + descent + margins.top + margins.bottom;

    // We've don't need to be remeasured anymore.
    remeasure = false;
  }

  /**
   * These methods tell the container how big the menubar wants to be.
   *  
   */
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  public Dimension getPreferredSize() {
    if (remeasure)
      measure();
    return prefsize;
  }

  /** @deprecated Here for compatibility with Java 1.0 */
  public Dimension minimumSize() {
    return getPreferredSize();
  }

  /** @deprecated Here for compatibility with Java 1.0 */
  public Dimension preferredSize() {
    return getPreferredSize();
  }

  /**
   * This method is called when the underlying AWT component is created. We
   * can't measure ourselves (no font metrics) until this is called.
   */
  public void addNotify() {
    super.addNotify();
    measure();
  }

  /** This method tells the container not to give us keyboard focus */
  public boolean isFocusTraversable() {
    return false;
  }

}

           
         
    
  
Related examples in the same category
1. Icon Demo Applet
2. Socket Applet
3. Applet Socket Quote
4. Applet Print
5. Applet System Properties
6. Applet Sound
7. Show Document
8. Applet communication (talk to each other)
9. Get Applets
10. JDBC applet
11. An application and an appletAn application and an applet
12. Applet and Swing ComponentsApplet and Swing Components
13. Icon behavior in JbuttonsIcon behavior in Jbuttons
14. Signed Applet
15. Load resource in Applet
16. Scribble AppletScribble Applet
17. Toggle buttonToggle button
18. Bouncing CircleBouncing Circle
19. Applet clock demoApplet clock demo
20. Applet event testerApplet event tester
21. Applet with parameters
22. First applet
23. AppletViewer - a simple Applet Viewer program
24. A simple loan calculator appletA simple loan calculator applet
25. URLButton -- a Button-like object that jumps to a URL
26. Get list of APPLET tags in one HTML file
27. Demonstration of Applet Methods
28. Demonstrates getParameterInfo() and getAppletInfo()
29. Walking Text Demo
30. Java Applets Can Run CGI's (on some browsers)
31. Demo Choice Applet
32. Demo Applet: Connect to Legacy System
33. ShowDocApplet: try out showDocument()
34. Bookmarks
35. Java Wave Applet Demo
36. Slide Puzzle
37. A class to allow use of the ncsa ISMAP format in java applets
38. File Read Applet
39. Applet I18N
40. Image Loader Applet
41. Thread Race Applet
42. Applet: Print from an Applet
43. Calling methods of an Applet from JavaScript code
44. Read an applet parameters
45. Change an applet background color
46. Passing Parameters to Java Applet
47. Display message in browser status bar
48. Your own Applet runtime environment
49. This applet is a simple button that plays an audio clip when pushed
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.