• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Popup Menu

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello all...
I want to place a popup menu when i right click the button.
Is it possible ? I tried with the below code but it is not showing any
button & is only showing a popup menu at the position i set in Show()
& when i click on the choices the button appears.
import java.awt.*;
import java.awt.event.*;
public class ACDemo extends Frame implements ActionListener
{
Button b;
TextArea t;
ACDemo()
{
setLayout(new FlowLayout());
b = new Button("Click");
add(b);
b.addActionListener(this);
TextField t = new TextField(10);
add(t);
PopupMenu p = new PopupMenu();
MenuItem m1 = new MenuItem("Hot");
MenuItem m2 = new MenuItem("Cold");
MenuItem m3 = new MenuItem("Spicy");
p.add(m1);
p.add(m2);
p.add(m3);
b.add(p);
setVisible(true);
p.show(this,52,52);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b)
{
System.exit(0);
}
}
public static void main(String args[])
{
ACDemo f = new ACDemo();
f.setBounds(100,100,400,400);
}
}
Can someone tell me where i m wrong ?
Thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I just tested by code, if you
addMouseListener to button b, then the popupmenu will appear (sure, you need event handler also)
Don't know if have other simple method to let it work.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to confirm, do we have to study PopupMenu for SCJP ?
Thanks in advance.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jini,
It's part of the AWT so you may get a question that requires knowledge of where and how it can be used.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
reply
    Bookmark Topic Watch Topic
  • New Topic