Popup Menu Applet

04/03/2002 update: version 1.1.2 is out (thanks for your feedback and suggestions).
Read about the new features below, and be sure to check out the FAQ
Also, try right-clicking anywhere on the page ;-)


Menu-enabled text label Menu-enabled picture Menu-enabled hyperlink Button-activated menu
Home Page

The above menus demonstrate some of the applet's features: redirection to a URL in the same window (Java and Perl menus), in a new window (JavaScript menu), and execution of various scripts.

These buttons demonstrate how JavaScript can call the applet's "public methods".


...whereas this checkbox demonstrates the opposite: its checked property can be changed by an inline JavaScript defined by the applet in a PARAM tag ("Inline script demo" on the popup menu):

Although inline scripts are a neat trick, it's usually easier to define a function in a SCRIPT tag of the HTML document, and then simply call that function in the applet's DATA PARAM tag ("Function call demo" on the popup menu).
It's easier to read, and gets around the problem of not being able to use braces or quotes that would otherwise break the PARAM tag syntax.
Click here to download the compiled class (that's all you need).
Click here for the source code, or here for the source, class and documentation.

Feel free to hack with the code and/or use it in your own Web pages (you don't have to link to my home page but I would appreciate an email if you do use my applet).
If you want to recompile the applet, make sure the Netscape class file (java30.zip or java40.jar required for JavaScripting) is on your CLASSPATH, and don't forget to use your browser's "super-reload" to reload the applet, and not just the HTML (SHIFT-Reload in Navigator, CTRL-Refresh in Explorer). Read the FAQ for more info on compiling the applet.

Applet Features

New in version 1.1.2

The previous version of the applet, version 1.1.1, is available here.

Requirements

Parameter Info

All parameter tags have HTML-like names and syntax, and are case-insensitive (except for JavaScript code).
URLs can be either absolute or relative.
Colors can be either one of the 13 basic color names (black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow), or Hexadecimal RGB values (as in HTML).
Fonts are limited to the fonts supported by the Java RunTime. The core fonts are Serif (TimesRoman), SansSerif (Helvetica), Monospaced (Courier), Dialog, DialogInput. Not all parameters are required: if the menu uses an image as its background, the parameters tags related to the text label are not necessary. If a text label is chosen, the IMG parameter must not be used.
How good the menus look and feel depends on how well your browser implements the JavaTM PopupMenu class. On Windows platforms, Navigator and HotJava do a very good job at it: menus look and sound like Windows menus (Desktop Themes and all). It works OK with Explorer 4.x, but much better with 5.x and 6.x.
On some versions of Unix such as Linux, Navigator and Mozilla use the background color (and foreground color with Mozilla), font style and size specified in the parameter tags (or the default values if the parameters are not specified).
Note: the Xroger icon (as in Jolly Roger for Unix X Windows) was created from a screen shot of the Xroger xscreensaver, with the help of the excellent ImageMagick editing tools.

FAQ

  1. How can I make the menu close on mouse out?
    Short answer: you can't. Long answer: JavaTM 1.1 popup menus, which this applet uses, do not have an API for that (application programming interface). They simply don't lose the mouse focus unless the user clicks on something. In most cases, the menu you see is really the native popup menu for your platform, invoked by the JavaTM Virtual Machine. If you right-click on your desktop, you'll observe the same behavior. That's because it's the same kind of menu. You may have seen other Java menus that support mouseOut-type behavior. They are not "true" popups, that is to say they cannot pop up outside of their applet area. The PopupMenu API is the only way to do that in JavaTM. If you want a true popup menu that supports onMouseOut, you have to use JavaScript/DHTML menus. They produce a nice effect, but are rather difficult to code, and have their own limitations.
  2. How can I make the menu items change the page in another frame?
    You have to specify the frame name as the target attribute of a menu item, in the DATA param. And don't call your frame by a reserved word, such as "main": call it mainFrame, or something like that. For example:
    {Look, Ma, another frame!*/your/link.html*mainFrame}
    or
    {Look, Ma, another frame!*/your/link.html*top.mainFrame}
    if you need to specify an absolute path to your frame.
  3. How can I change the foreground and background color of the popup?
    Short answer: there is no JavaTM API to specify background and foreground colors for a Menu object, so it shouldn't be possible in theory. Long answer: it depends on the operating system. On Windows, menu colors seem to be defined by the desktop themes (but that may even vary across browser and OS versions). On Linux, you can actually do it: the foreground and background colors for the menu are those of the applet itself, which you can specify in the BGCOLOR and TEXT params. I don't know about other Unices, or about MACs (and it could be different for OS 9 and FreeBSD-based OS X).
  4. How can I put the applet on my website with FrontPage?
    I don't know how any particular WYSIWYG HTML "editor" does it. I use a text editor (Vim) for all HTML coding. Just open your HTML file with your favorite text editor and add the code manually. You can copy and paste some code from my website as a starting point, and modify it according to your needs. You also have to copy the class file to your website.
  5. I put the applet on my website, and it doesn't work. Now what?
    Make sure the location of the class file specified in your HTML matches the location on your Web server, and that permissions are correct. Check the JavaTM console in your browser for any errors, as well as your website's error log. Also make sure that the braces in your DATA param are balanced: every opening brace must have a closing brace. If you still can't get it to work, send me a zip file or compressed tarball with all necessary files, and I'll try to point you in the right direction.
  6. I have several applets on my website, but the popup always appear from the same applet. What's wrong?
    You need to make sure each applet has a unique name on your page (e.g. PopupMenuAplet1, PopupMenuAplet2, etc.) in the NAME attribute of the APPLET tag.
  7. How can I use the applet in a rollover?
    Put a zero-size applet next to your rollover image, and have the JavaScript code in your rollover script call the applet's popup() or popup(x,y) function.
  8. How can I use the applet in a navbar? Can I use an application-style (File, Edit, etc.) menu bar?
    JavaTM applets cannot use menu bars on a web page, because MenuBar objects must be attached to a frame, so they need to be in their own window (yeah, I know, there's a hack around that, but it only works on some browsers). For a navbar effect, you'll need several applets next to one another. You might want to put them in an HTML table, and specify mousePressed as the only value for the MOUSEEVENTS param (see param information for details).
  9. How can I use the applet with an image map?
    You need to place one zero-size applet for each menu you'll need next to your image map, and have the target for each of your map areas be a "javascript:" type URL calling the appropriate applet's popup(x,y) function. You just have to work out the proper x,y coordinates to call the popup with.
  10. How can I change the font/style/size of individual menu items?
    The current version of the applet doesn't support that, although it would certainly be possible to add that functionality. It would probably require a different syntax / parsing routine for the menu items, though, which would break backwards compatibility, so I'd be hesitant to do it unless there's a big demand for it.
  11. How can I change the direction of the popup?
    Sorry, you can't specify how the menu should pop up (up, down, left or right). There's no API for that.
  12. How can I add icons to my menus?
    Sorry, again, there's no API for that. However that is possible with Swing menus (JavaTM 2 and higher). Unfortunately, at this time, Swing support for browsers is not very widespread. IE doesn't support it, for example. However as more and more browsers are starting to support Java via Sun's Java plugin (Mozilla, Opera), it might be worth writing a Swing version of the applet.
  13. How can I compile the applet? I'm getting an error with "import netscape.javascript.*".
    Your compiler needs to know where to find the JSObject classes. You can either add to your classpath the JAR file that contains them (located in your Netscape directory tree: java40.jar for Netscape 4.*, javaplugin.jar for Sun's plugin), or use this
    JSObject.jar file I created from javaplugin.jar, with just what's needed to compile the applet. And don't forget to set the target type to 1.1 if you use a recent compiler. Here's the command I use:
    /usr/local/j2sdk1.4.0/bin/javac -g:none -O -target 1.1 -classpath JSObject.jar PopupMenuApplet.java
  14. Can I change the menus in a running applet?
    Changing the DATA value from within the page would not work on a running applet: the applet would have to be "super-reloaded", with SHIFT-Reload, or CTRL_Refresh, depending on your browser.
    To change any param in an applet, you basically have to write a new page (or frame), so that the browser knows it's something brand new. You can do that dynamically either on the server side (CGI style) or on the client side with JavaScript.
    In theory, you could alter the menu items from a JavaScript while the applet is running. This would require that those objects be declared as "public" objects, so that they could be accessed from outside the applet. But because those objects are stored in Java arrays and not just simple variables, the browser must have good support for the LiveConnect technology, which is a kind of Java/JavaScript bridge developed by Netscape. The problem is that Internet Explorer has a very poor implementation of LiveConnect, and it just doesn't work. Netscape 4.* used to have a great implementation of it (they invented it, after all), but it got totally broken in 6.* when they switched to Mozilla before it was ready for prime time. It is now near full support again, with Mozilla 0.9.9, though...
Home JavaTM Javascript Perl