Solved

Hiding a JPopupMenu and opening a new one

Posted on 2001-06-25
6
420 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

617 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