Solved

Create popup window similar to menu or combo drop down

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

691 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