Solved

Hiding a JPopupMenu and opening a new one

Posted on 2001-06-25
6
410 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
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
network + 7 73
wordlen challenge 3 47
java.io.BufferedReader   readLine()  //returns the entire file ? 4 34
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now