I independently invented the same solution as described in How to create a JButton with a menu? for creating a popup menu in a Swing applet. However, the coordinate specified in popup.show() is where the upper-left of the popup menu will be. I want the lower-left of the menu to coincide with the upper left corner of my button. This is the same as the layout older Windows versions use for the Start menu relative to the Start button.
What I have is this:
JPopupMenu popup_menu;
JButton button;
public void actionPerformed(ActionEvent e)
{
int menu_height = this.popup_menu.getHeight();
this.popup_menu.show(this.button, 0, -menu_height);
}
The problem is that the first time the button is clicked, it seems menu_height isn't properly initialized and the menu appears in the bottom right. However on subsequent clicks it's okay. The best ideas I can come up with are
showing the JPopupMenu when the applet starts up, then hiding it after a 1ms delay.
writing my own Swing menu component
Anyone have ideas that are more elegant than the first but less work than the second?