Results 1 to 5 of 5
  1. #1
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    Default JPopupMenu falls behind window

    hello, i seem to be having some trouble here. I am writing a little class that is a popupSlider widget. The class extends JLabel so the user would treat this component like a label. When the label is left clicked on a jpopupMenu with a slider in it appears. This works all well and good except the second i click on the slider, i still have control of the slider (i can drag it up and down), the popup menu then disappears behind the rest of the window. I'm thinking i am missing something when processing the click event or the release events.

    (yea there is probably some ugly code here, but it is in development, i want to get the thing working then clean up the code.)

    couple things to note:
    'popup' is a JPopupMenu object with a JSlider simply 'add()'ed to it
    Java Code:
        public void mouseClicked(MouseEvent e)
        {
            System.out.println("clicked " + e.getSource().getClass() );
            if(e.getButton() == MouseEvent.BUTTON1)
            {
                Point pos = getMousePosition();
                pos.x += getLocationOnScreen().x;
                pos.y += getLocationOnScreen().y;
                
                popup.setLocation(pos);
                popup.setVisible(true);
            }
        }
        public void mouseReleased(MouseEvent e)
        {
            System.out.println("released " + e.getSource().getClass() );
            if( (e.getButton() == MouseEvent.BUTTON1) &&
                (e.getSource().getClass() == JSlider.class) )
            {
                System.out.println("mouse released on slider");
                popup.setVisible(false);
            }
        }
    any help would be appreciated.
    -Undertow

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    the popup menu then disappears behind the rest of the window
    Sounds like the "rest of the window" is or contains AWT/heavyweight components. Heavyweight components are always rendered above/on_top_of/in_the_way_of lightweight components. See Mixing heavy and light components for discussion.

  3. #3
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    Default

    Strange, apparently you cant have a JPopupMenu appear on top of a JFrame. Doesn't make a lot of sense to me. The thing appears when i click but as soon as i click to drag the slider the popup goes behind the JFrame. guess i am SOL. strange though, i've seen a package out there that is just this thing i am trying to do (i just don't want to pay for it). so i am assuming it is possible...

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    cant have a JPopupMenu appear on top of a JFrame
    Can you add the popup to a lightweight child (of the frame) component or perhaps the JRootPane of the JFrame?

  5. #5
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    Talking PopupSlider solved.

    Looks like YES you can have a JPopupMenu appear on top of a JFrame or a JPanel etc. etc. Looks like the key is implementing the MenuElement interface.

    Java Code:
    public class PopupSlider extends JSlider implements MenuElement
    {
        private JPopupMenu menu;
        
        public PopupSlider()
        {
            menu = new JPopupMenu();
            
            setMajorTickSpacing(10);
            setMinorTickSpacing(5);
            setPaintLabels(false);
            setPaintTicks(true);
            setValue(0);
            setPaintTrack(true);
            setInverted(false);
            setOrientation(JSlider.VERTICAL);
            setSize(getPreferredSize());
    
            addMouseListener(new MouseAdapter()
            {
                public void mouseReleased(MouseEvent e)
                {
                    setVisible(false);
                }
            });
            
            menu.add(this);
        }
        
        public void setLocation(int x, int y)
        {
            menu.setLocation(x,y);
        }
        
        public void setVisible(boolean v)
        {
            menu.setVisible(v);
        }
        
        public boolean isMenuVisible()
        {
            return menu.isVisible();
        }
    
        public void processMouseEvent(MouseEvent e, MenuElement path[], MenuSelectionManager manager)
        {
        }
    
        public void processKeyEvent(KeyEvent e, MenuElement path[], MenuSelectionManager manager)
        {
        }
    
        public void menuSelectionChanged(boolean isIncluded)
        {
        }
    
        public MenuElement[] getSubElements()
        {
            return new MenuElement[0];
        }
    
        public Component getComponent()
        {
            return this;
        }
    
    }
    Using this class one could say; create a JLabel somewhere and add a mouse listener to it and call PopupSlider.setVisible() to make it appear. If you want to try something, first try it without modification and see that the slider stays ontop while you are working with it. Then go to the class and comment out the "implements MenuElement" and run again. This time you will see that the slider falls behind the window as soon as you start to use it.

    So if anyone wants a nice little popup slider they can use and modify this class instead of paying the $15 or $20 for the JPopupSlider on the web.

    Also if anyone cares, you could use a JDialog that is undecorated instead of a JPopupMenu to get cool bonuses like "always on top"

    :D
    Last edited by undertow; 01-22-2008 at 04:30 AM. Reason: add a comment about interface implmentation

Similar Threads

  1. drawing window
    By BlitzA in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 12:55 PM
  2. refresh a different window
    By marceldupont in forum AWT / Swing
    Replies: 3
    Last Post: 03-22-2008, 02:44 AM
  3. disable parent window
    By ismailsaleh in forum AWT / Swing
    Replies: 1
    Last Post: 01-07-2008, 11:15 PM
  4. drawing window
    By BlitzA in forum Advanced Java
    Replies: 0
    Last Post: 12-30-2007, 05:39 PM
  5. dynamising the height of a JPopupMenu
    By iimasd in forum AWT / Swing
    Replies: 6
    Last Post: 11-21-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •