aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PopupMenu in Menu? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PopupMenu in Menu?" Watch "PopupMenu in Menu?" New topic
Author

PopupMenu in Menu?

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
Hello.
as far as I understand, objects of MenuItem and its subclasses can be
added to a Menu. that means PopupMenu can be added to a Menu too since
PopupMenu is a subclass of MenuItem. so I wrote the following test
code and it compiles. but I couldn't see the popup menu showing on my
menubar. can anyone tell me why? thanks a lot.
cw
===============================
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class A extends Frame {
public static void main(String[] args) {
Frame a = new A();
MenuBar mb = new MenuBar();
Menu m = new Menu("Menu");
CheckboxMenuItem cbmi = new CheckboxMenuItem("check");
MenuItem mi1 = new MenuItem("first");
MenuItem mi2 = new MenuItem("second");
PopupMenu pm = new PopupMenu("Pop");
pm.add("pop1");
pm.add("pop2");
m.add(cbmi);
m.add(mi1);
m.add(mi2);
m.add(pm);
mb.add(m);
a.setMenuBar(mb);
a.setSize(250,250);
a.setVisible(true);
}
}
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Chun,
You added the Popup menu to 'menu' not the menubar.
Change 'm.add(pm)' to 'mb.add(pm)
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
Thanks Jane!
but can I have a Popupmenu nested in a Menu? if so, how? please help. thanks.
chun
Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
Hi, Jane.
I just tried adding popupmenu to the menubar. but the whole menu became a popup menu instead of a pull-down menu. my question is can you have a pull-down menu as well as a popupmenu in the menubar? can you have a popupmenu nested in a pull-down menu? if so, how? thanks a lot.
chun
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
Hi, chun.
I tried your code and Jane's suggestion. The pop-up menu can be added to menu and menubar. What version of JVM do you use?
Zheng
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Chun,
You're example had the popupmenu as part of the menu in the menubar. You can use a popupmenu anywhere you would use a menu.
Here's an example I fudged from examples in the Class Libraries

Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PopupMenu in Menu?