Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

PopupMenu in Menu?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane!
but can I have a Popupmenu nested in a Menu? if so, how? please help. thanks.
chun
 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!