Link to home
Start Free TrialLog in
Avatar of buri
buri

asked on

Hiding a JPopupMenu and opening a new one

Hi,

When a user clicks the RMB, a JPopupMenu appears. If the user clicks again elsewhere without closing the old menu, a new one should appear in the new location (i.e. the exact behaviour of the Windows desktop) Here is my relevant code:

JPopupMenu popup;

public void mousePressed(MouseEvent e) {
   if ((SwingUtilities.isRightMouseButton(e)) {
      if (popup != null && popup.isVisible()) {
         System.out.println("Hiding old popup");
         popup.setVisible(false);
         popup = null;
     }
    System.out.println("Showing new popup.");
    popup = new JPopupMenu();
    popup.show((Component) e.getSource(), e.getX(), e.getY());
}

Results:
The initial poup comes up fine.
Clicking RMB elsewhere outputs "Hiding old popup. Showing new popup." but no new menu is produced. Sometimes, I can see the brief flash of the new menu but it never sticks around.

Any help would be appreciated, thanks!
BU
Avatar of superschlonz
superschlonz

Add the popup to the JComponent in which it should pop up, for
example a JTable: table.add( popup );
Then you can in the method processMouseEvent() just check if
it is a popup trigger:
public void processMouseEvent( MouseEvent event )
{
  if( event.isPopupTrigger() )
  {
    popup.show( event.getComponent(), event.getX(), event.getY() );
  }
}
Avatar of buri

ASKER

This popup menu might not necessarily be associated with any particular Component.

Thanks,
BU
Hi,
you can use your own Popup menu and override the objects' listener with your class.
Ex:

import javax.swing.*;
import java.awt.event.*;

public class MyDialog extends JDialog
{
     public static void main(String[] args)
     {
          MyDialog my = new MyDialog();
          my.init();
     }
     
     private void init() {
         
          try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Exception ex) {
          }
         
          RightClick rc = new RightClick();
          addMouseListener(rc);
          setTitle("Right Click Application");
         
          addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                    setEnabled(false);
                    dispose();
                    System.exit(0);
          }});
               
          pack();
          setSize(200,200);
          show();
     }
     
     public class RightClick extends MouseAdapter {
         
          JPopupMenu popup;
          JMenuItem one;
          JMenuItem two;
          JMenuItem three;
          JMenuItem four;
          JMenuItem five;
          JMenuItem six;
         
          public RightClick() {
               popup     = new JPopupMenu();
               one          = new JMenuItem("one");
               two          = new JMenuItem("two");
               three     = new JMenuItem("three");
               four     = new JMenuItem("four");
               five     = new JMenuItem("five");
               six          = new JMenuItem("six");
                             
               // add actionlisteners to your menu items
               // one.addActionListener( ... );
               
               popup.add(one);
               popup.add(two);
               popup.addSeparator();
               popup.add(three);
               popup.add(four);
               popup.addSeparator();
               popup.add(five);
          }
         
         
          public void mousePressed(MouseEvent e) {
                    ShowPopupMenu(e);
        }

        public void mouseReleased(MouseEvent e) {
                    ShowPopupMenu(e);
        }

        private void ShowPopupMenu(MouseEvent e) {
            if (e.isPopupTrigger())
                    popup.show(e.getComponent(),
                                 e.getX(), e.getY());
          }
     }
}



regards,
dbaora.
Avatar of buri

ASKER

Hi,

I tried variations on this earlier. The problem isn't bringing up the popup, it's bringing up a new one when an old one was just on the screen. Both methods of bringing up popups are sound, but they run into the same problem that I'm encountering.

Thanks,
BU
I had similar problems with the popup until I added it to a JComponent
and opened it like in my example.
I'm not sure what happens if you add it to the JFrame which contains all
the other components. Perhaps all the components show it also ?
ASKER CERTIFIED SOLUTION
Avatar of Igor Bazarny
Igor Bazarny
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial