Solved

Hiding a JPopupMenu and opening a new one

Posted on 2001-06-25
6
417 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:buri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6224459
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() );
  }
}
0
 

Author Comment

by:buri
ID: 6224506
This popup menu might not necessarily be associated with any particular Component.

Thanks,
BU
0
 
LVL 2

Expert Comment

by:dbaora
ID: 6224532
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:buri
ID: 6224616
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
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6224837
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 ?
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 75 total points
ID: 6225165
Hi,

Try to show second popup via SwingUtilities.invokeLater()--give swing time to handle events remaining in queue.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 60
jsp insert to database example 2 65
Is there a simpler dropbox system? 10 44
Html split(text) 2 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question