/** * Retrieves the layer index of the decorated component in the layered pane of the window. * * @param layeredPane Layered pane of the window. * * @return Index of the decorated component in the layered pane. */ private Integer getDecoratedComponentLayerInLayeredPane(JLayeredPane layeredPane) { Container ancestorInLayer = decoratedComponent; while (!layeredPane.equals(ancestorInLayer.getParent())) { ancestorInLayer = ancestorInLayer.getParent(); } return (layeredPane.getLayer(ancestorInLayer) + DECORATION_LAYER_OFFSET); }
public static class MouseBlockerUI extends LayerUI<JComponent> { @Override protected void processMouseEvent(MouseEvent e, JLayer l) { JLayeredPane layeredPane = (JLayeredPane) l.getParent(); if (layeredPane.getLayer(l) != layeredPane.highestLayer()) e.consume(); } @Override public void installUI(JComponent c) { super.installUI(c); JLayer jlayer = (JLayer)c; jlayer.setLayerEventMask( AWTEvent.MOUSE_EVENT_MASK ); } @Override public void uninstallUI(JComponent c) { JLayer jlayer = (JLayer)c; jlayer.setLayerEventMask(0); super.uninstallUI(c); } }
/** Returns whether the given Component is the content pane for a {@link RootPaneContainer}. @see javax.swing.RootPaneContainer#getContentPane */ public static boolean isContentPane(Component c) { if (c.getParent() instanceof JLayeredPane) { JLayeredPane p = (JLayeredPane)c.getParent(); if (p.getParent() instanceof JRootPane) { return ((JRootPane)p.getParent()).getContentPane() == c; } int layer = JLayeredPane.FRAME_CONTENT_LAYER.intValue(); return p.getLayer(c) == layer && !(c instanceof JMenuBar); } return false; }
/** Returns whether the given component is a lightweight popup, that is, a container for a JPopupMenu that is implemented with a lightweight component (usually JPanel). */ public static boolean isLightweightPopup(Component c) { if (c instanceof JPanel) { Window w = SwingUtilities.getWindowAncestor(c); if (w != null && isHeavyweightPopup(w)) return false; JPanel panel = (JPanel)c; Container parent = panel.getParent(); if (parent != null) { if (parent instanceof JLayeredPane) { int layer = JLayeredPane.POPUP_LAYER.intValue(); if (JLayeredPane.getLayer(panel) == layer) return true; } } return panel.getComponentCount() == 1 && panel.getComponents()[0] instanceof JPopupMenu; } return false; }
@Override public void eventDispatched(AWTEvent ev) { if (!(ev instanceof MouseEvent) || !(ev.getSource() instanceof Component)) { // We are interested in MouseEvents only return; } Component src = (Component) ev.getSource(); // Close popup only on mouse press on a component which has // the same window ancestor as our popup, but is not in the // popup layer of the window. if (ev.getID() == MouseEvent.MOUSE_PRESSED) { if (SwingUtilities.getWindowAncestor(src) == SwingUtilities.getWindowAncestor(JPopupButton.this)) { JLayeredPane srcLP = (JLayeredPane) SwingUtilities.getAncestorOfClass(JLayeredPane.class, src); Component srcLPChild = src; while (srcLPChild.getParent() != srcLP) { srcLPChild = srcLPChild.getParent(); } if (srcLP.getLayer(srcLPChild) < JLayeredPane.POPUP_LAYER) { popupMenu.setVisible(false); } } } else { } } };
JButton button = new JButton(" Menu \u25be "); JFrame frame = new JFrame("Title"); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); // Force creation of title bar components. frame.setPreferredSize(new Dimension(400, 400)); frame.pack(); JLayeredPane layeredPane = frame.getLayeredPane(); JComponent titlePane = (JComponent) layeredPane.getComponentAt(0, 0); for (Component c : titlePane.getComponents()) { if (c instanceof Container && ((Container) c).getLayout() instanceof DefaultMenuLayout) { int layer = layeredPane.getLayer(titlePane); layeredPane.setLayer(button, layer + 1); button.setLocation(c.getX() + c.getWidth() + 3, 1); Dimension size = button.getPreferredSize(); size.height = Math.min(size.height, titlePane.getHeight() - 2); button.setSize(size); layeredPane.add(button); break; } }
srcLPChild = srcLPChild.getParent(); if (srcLP.getLayer(srcLPChild) < JLayeredPane.POPUP_LAYER) { JComponentPopup.this.setVisible(false);
srcLPChild = srcLPChild.getParent(); if (srcLP.getLayer(srcLPChild) < JLayeredPane.POPUP_LAYER) { JComponentPopup.this.setVisible(false);
if (comp instanceof JInternalFrame.JDesktopIcon) { icons.add(comp); maxLayer = Math.max(getLayer(comp), maxLayer);
int layer = JLayeredPane.getLayer(f); JLayeredPane.putLayer(desktopIcon, layer);