?
Solved

Create popup window similar to menu or combo drop down

Posted on 2006-10-29
4
Medium Priority
?
288 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
[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
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 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