The following application attempts to create a Popup menu.
It works as expected on Solaris.
On Windows NT, the menu doesn't popup when the right button is
pressed.
import java.awt.*;
import java.awt.event.*;
class PopupMenuBug extends Frame {
PopupMenu pm;
MenuItem pmi;
PopupMenuTrigger pmt;
public PopupMenuBug() {
setLayout(new BorderLayout());
pm = new PopupMenu();
pmi = new MenuItem("I'm a MenuItem in a PopupMenu");
pm.add(pmi);
pmt = new PopupMenuTrigger(pm);
add("South", pmt);
pmi.addActionListener(new MyActionListener());
pack();
show();
}
public static void main(String[] args) {
new PopupMenuBug();
}
}
//class PopupMenuTrigger extends Canvas implements MouseListener {
class PopupMenuTrigger extends Component implements MouseListener {
PopupMenu pm;
public PopupMenuTrigger(PopupMenu pm) {
this.pm = pm;
addMouseListener(this);
add(pm);
}
public void mousePressed(MouseEvent e) {
System.out.println(e);
System.out.println("e.isPopupTrigger(): " + e.isPopupTrigger());
if(e.isPopupTrigger()) {
pm.show(this, e.getX(), e.getY());
}
};
public void mouseClicked(MouseEvent e) {};
public void mouseReleased(MouseEvent e) {};
public void mouseEntered(MouseEvent e) {};
public void mouseExited(MouseEvent e) {};
public void paint(Graphics g) {
g.setColor(Color.red);
g.fill3DRect(0,0,getSize().width-1, getSize().height-1, true);
g.setColor(Color.black);
g.drawString("Right Click Here", 0, getSize().height/2);
}
public Dimension getPreferredSize() {
Graphics g = getGraphics();
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth("Right Click Here");
g.dispose();
return new Dimension(w, 50);
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("actionPerformed: " + e);
}
}