Results 1 to 5 of 5
Thread: JPopupMenu falls behind window
- 01-15-2008, 07:24 PM #1
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
any help would be appreciated.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); } }
-Undertow
- 01-16-2008, 02:27 AM #2
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.
- 01-16-2008, 06:01 PM #3
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...
- 01-16-2008, 10:45 PM #4
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?
- 01-17-2008, 10:20 PM #5
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.
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.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; } }
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"
:DLast edited by undertow; 01-22-2008 at 04:30 AM. Reason: add a comment about interface implmentation
Similar Threads
-
drawing window
By BlitzA in forum New To JavaReplies: 1Last Post: 01-15-2009, 12:55 PM -
refresh a different window
By marceldupont in forum AWT / SwingReplies: 3Last Post: 03-22-2008, 02:44 AM -
disable parent window
By ismailsaleh in forum AWT / SwingReplies: 1Last Post: 01-07-2008, 11:15 PM -
drawing window
By BlitzA in forum Advanced JavaReplies: 0Last Post: 12-30-2007, 05:39 PM -
dynamising the height of a JPopupMenu
By iimasd in forum AWT / SwingReplies: 6Last Post: 11-21-2007, 10:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks