Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Applets
Expertise: Beginner
Mar 20, 1997

Can I Add Menu Bars to Applets?

Question:
Is it possible to add a menu bar to an applet without using Frame as the container?

Also, is it possible to show a Frame without a new window popping up?

I am building an applet for the Web, and I would like to run this applet on Netscape without a new window showing up. But I also want a menu bar in the applet. What are my options?

Answer:
Unfortunately, it is not possible to make effective use of a menubar in an applet's page. As you've discovered, the only way to attach a menubar to anything is via the Frame.setMenuBar() method. And since Frames are pop-out windows and detach from the browser when they are displayed, you can't attach the menubar to the applet right on the browser window itself.

Having said that, I should also tell you about a hack that can let you force a menubar on an applet's page, though it doesn't let you do very much with it. You see, an applet is contained within a Frame on the browser's window (usually a browser-specific subclass of the Frame Object.) So if you somehow get hold of the applet's container Frame, you can call all the normal Frame methods on it.

Well, you can do this by starting with the applet and repeatedly asking for its container object until you get the one that is a subclass of Frame. You can do this with the Component.getParent() method.

For example, the following applet attaches a text area with a simple menubar to the applet right on the page:

import java.awt.*;
import java.applet.*;

public class AppletMenu extends Applet {
       TextArea textarea;

       public void init() {

               // create a  menubar
               with some menuitems

               MenuBar menubar = new MenuBar();
               Menu menu = new Menu("File");
               Menu submenu1 = new Menu("Open");
               MenuItem item1 = new MenuItem("File");
               MenuItem item2 = new MenuItem("URL");
               submenu1.add(item1);
               submenu1.add(item2);
               menu.add(submenu1);

               Menu submenu2 = new Menu("Save As");
               MenuItem item3 = new MenuItem("text");
               MenuItem item4 = new MenuItem("html");
               submenu2.add(item3);
               submenu2.add(item4);
               menu.add(submenu2);
               menubar.add(menu);

               Component c = this;
               while (c != null && !(c instanceof Frame)) {
                       c = c.getParent();
               }
               ((Frame)c).setMenuBar(menubar);

               textarea = new TextArea(10, 30);
               setLayout(new BorderLayout());
               add("Center", textarea);
       }

       public boolean handleEvent(Event e) {
               if (e.target instanceof MenuItem) {
                       MenuItem item = (MenuItem) e.target;
                       textarea.appendText(item.getLabel() + "\n");
                       return true;
               }
               return false;
       }
}

However, this hack isn't very useful because the applet no longer receives Events generated by the MenuBar object. From the handleEvent routine above, you can see that the applet is set up to collect all MenuItem events and print them on the textarea, but that doesn't happen. Those Events go to the applet's parent widgets instead which simply ignore them. So while you can have a menubar, you can't tell if it's being used.

This hack is not entirely useless. You can use other Frame methods once you've acquired the container Frame. For example, you use it to set the Applet's mouse pointer icon, with:

       public void setCursor(int cursorType)
where cursorType is one of the following:
       public final static int CROSSHAIR_CURSOR;
       public final static int DEFAULT_CURSOR;
       public final static int E_RESIZE_CURSOR;
       public final static int HAND_CURSOR;
       public final static int MOVE_CURSOR;
       public final static int N_RESIZE_CURSOR;
       public final static int NE_RESIZE_CURSOR;
       public final static int NW_RESIZE_CURSOR;
       public final static int S_RESIZE_CURSOR;
       public final static int SE_RESIZE_CURSOR;
       public final static int SW_RESIZE_CURSOR;
       public final static int TEXT_CURSOR;
       public final static int W_RESIZE_CURSOR;
       public final static int WAIT_CURSOR;
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date