Solved

Create popup window similar to menu or combo drop down

Posted on 2006-10-29
4
258 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
ID: 17828954
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
ID: 17830555
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
ID: 17830847
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
ID: 17830961
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 79
Checkbox and ListView in Android Layout 4 56
javap bin 2 29
Windows 10 IE Certificate Issue 10 42
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)
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…
This video teaches viewers about errors in exception handling.
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 …

895 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

13 Experts available now in Live!

Get 1:1 Help Now