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.

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;

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as