Solved

Create popup window similar to menu or combo drop down

Posted on 2006-10-29
4
243 Views
Last Modified: 2008-02-01
I am trying to create a window that behaves like a combo drop down or menu drop down. It needs to appear when the user clicks a button and disappear when (A) something in it is selected on it or (B) the user clicks on anything else.

(B) is the piece that is giving me trouble. I have added a focus listener and put a setVisible(false) if the window loses focus, so this works sometimes, but not if the user clicks on a component that is not focusable.

How can this be done? Please provide example code if possible.
0
Comment
Question by:harlanhugh
  • 2
4 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
From the Almanac:

   final JPopupMenu menu = new JPopupMenu();
   
    // Create and add a menu item
    JMenuItem item = new JMenuItem("Item Label");
    item.addActionListener(actionListener);
    menu.add(item);
   
    // Set the component to show the popup menu
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

;JOOP!
0
 
LVL 1

Accepted Solution

by:
spyderhydrant earned 500 total points
Comment Utility
Most programmers use the general convention that pop-up windows (dialogs) don't go away until the user selects something or clicks "cancel".
That's why the functionality you are looking for isn't so common, but that doesn't mean you can't do it!

I see what you are trying to do. So basically, you want to generate an event that corresponds to "clicking on anything besides the pop-up".

There aren't any good shortcuts: you will want to add a MousePressed listener to everything besides the popup.
I wrote some code that makes this really easy to do using a recursive function.

** FIRST add this to your fields:

      private MouseListener outsideClickListener = new MouseAdapter(){
            
            public void mousePressed(MouseEvent arg0) {
                  
                  // Code to set your popup.visible to false
                  System.out.println("Outside Click"); // test that it's working
            }            
            
      };

** THEN add this function to you class

      public void addListenerToAll(Component c)
      {
            
            c.addMouseListener(outsideClickListener);
            
            if(c instanceof Container){
                  Component[] components = ((Container)c).getComponents();
                  for(int i = 0; i < components.length; i++)
                  {
                        addListenerToAll(components[i]);            
                  }
            }      
      }

** LASTLY, call the function on your main frame (Window) *after you have added all components to it (buttons, panels, etc.)*:

     addListenerToAll(yourMainFrame); // if your class extends JFrame, yourMainFrame is "this"


I tested the code out, and it works well. Good luck; let me know if you have any questions.
0
 

Author Comment

by:harlanhugh
Comment Utility
spyderhydrant, very nicely done. This works almost perfectly. The only problem is that clicking on the frame's title bar still will not generate a response since java doesn't seem to process events from there. Thanks for the excellent solution.
0
 
LVL 1

Expert Comment

by:spyderhydrant
Comment Utility
Thank you! :)

I did a little research on the title-bar event issue. Unfortunately it seems there's no easy way to get the title bar to generate an event when clicked (title bars are controlled by the OS). Some forums say it's impossible, but I don't believe that; it might just take some ugly code and clever hacks.

Good luck with everything, and thanks again for the points!
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
countAbc challenge 9 49
What is JNDI datasource in spring 1 37
groovy example issue 10 65
unix in java example 9 38
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

16 Experts available now in Live!

Get 1:1 Help Now