Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hiding a JPopupMenu and opening a new one

Posted on 2001-06-25
6
Medium Priority
?
422 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 225 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

705 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