Solved

Create popup window similar to menu or combo drop down

Posted on 2006-10-29
4
266 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

813 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

15 Experts available now in Live!

Get 1:1 Help Now