devxlogo

Can I Add Menu Bars to Applets?

Can I Add Menu Bars to Applets?

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

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

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

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

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

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

For example, the following applet attaches a text area with a simplemenubar 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() + ”
“); return true; } return false; }}
However, this hack isn’t very useful because the applet no longerreceives Events generated by the MenuBar object. From the handleEventroutine above, you can see that the applet is set up to collect all MenuItemevents and print them on the textarea, but that doesn’t happen.Those Events go to the applet’s parent widgets instead which simplyignore them. So while you can have a menubar, you can’t tell ifit’s being used.
See also  Why ChatGPT Is So Important Today

This hack is not entirely useless. You can use other Frame methodsonce you’ve acquired the container Frame. For example, you use itto 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;

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist