Java Code Examples for java.awt.Component#getParent()

The following examples show how to use java.awt.Component#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroupLayout.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Replaces an existing component with a new one.
 *
 * @param existingComponent the component that should be removed
 *        and replaced with {@code newComponent}
 * @param newComponent the component to put in
 *        {@code existingComponent}'s place
 * @throws IllegalArgumentException if either of the components are
 *         {@code null} or {@code existingComponent} is not being managed
 *         by this layout manager
 */
public void replace(Component existingComponent, Component newComponent) {
    if (existingComponent == null || newComponent == null) {
        throw new IllegalArgumentException("Components must be non-null");
    }
    // Make sure all the components have been registered, otherwise we may
    // not update the correct Springs.
    if (springsChanged) {
        registerComponents(horizontalGroup, HORIZONTAL);
        registerComponents(verticalGroup, VERTICAL);
    }
    ComponentInfo info = componentInfos.remove(existingComponent);
    if (info == null) {
        throw new IllegalArgumentException("Component must already exist");
    }
    host.remove(existingComponent);
    if (newComponent.getParent() != host) {
        host.add(newComponent);
    }
    info.setComponent(newComponent);
    componentInfos.put(newComponent, info);
    invalidateHost();
}
 
Example 2
Source File: RoundedRectanglePopup.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
private void initPopup(Component owner, Component contents, int x, int y, Popup popup) {
	this.owner = owner;
	this.contents = contents;
	this.popup = popup;
	this.x = x;
	this.y = y;

	boolean mac = false;
	try {
		mac = System.getProperty("os.name").toLowerCase().startsWith("mac");
	} catch (SecurityException e) {
		// do nothing
	}
	if (mac) {
		((JComponent) contents).setBorder(Borders.getPopupMenuBorder());
	} else if (((JComponent) contents).getBorder() instanceof DummyBorder) {
		if ((owner != null) //
				&& (((owner instanceof JMenu) && ((JMenu) owner).isTopLevelMenu()) //
				|| ((owner.getParent() != null) && (owner.getParent() instanceof javax.swing.JToolBar)) //
				|| (owner instanceof javax.swing.JComboBox))) {
			((JComponent) contents).setBorder(Borders.getPopupBorder());
		} else {
			((JComponent) contents).setBorder(Borders.getShadowedPopupMenuBorder());
		}
	}
}
 
Example 3
Source File: LegacyGlueFocusTraversalPolicy.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
private boolean accept(Component aComponent) {
    if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
          aComponent.isFocusable() && aComponent.isEnabled())) {
        return false;
    }

    // Verify that the Component is recursively enabled. Disabling a
    // heavyweight Container disables its children, whereas disabling
    // a lightweight Container does not.
    if (!(aComponent instanceof Window)) {
        for (Container enableTest = aComponent.getParent();
             enableTest != null;
             enableTest = enableTest.getParent())
        {
            if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
                return false;
            }
            if (enableTest instanceof Window) {
                break;
            }
        }
    }

    return true;
}
 
Example 4
Source File: AncestorNotifier.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
void addListeners(Component ancestor, boolean addToFirst) {
    Component a;

    firstInvisibleAncestor = null;
    for (a = ancestor;
         firstInvisibleAncestor == null;
         a = a.getParent()) {
        if (addToFirst || a != ancestor) {
            a.addComponentListener(this);

            if (a instanceof JComponent) {
                JComponent jAncestor = (JComponent)a;

                jAncestor.addPropertyChangeListener(this);
            }
        }
        if (!a.isVisible() || a.getParent() == null || a instanceof Window) {
            firstInvisibleAncestor = a;
        }
    }
    if (firstInvisibleAncestor instanceof Window &&
        firstInvisibleAncestor.isVisible()) {
        firstInvisibleAncestor = null;
    }
}
 
Example 5
Source File: LegacyGlueFocusTraversalPolicy.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private boolean accept(Component aComponent) {
    if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
          aComponent.isFocusable() && aComponent.isEnabled())) {
        return false;
    }

    // Verify that the Component is recursively enabled. Disabling a
    // heavyweight Container disables its children, whereas disabling
    // a lightweight Container does not.
    if (!(aComponent instanceof Window)) {
        for (Container enableTest = aComponent.getParent();
             enableTest != null;
             enableTest = enableTest.getParent())
        {
            if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
                return false;
            }
            if (enableTest instanceof Window) {
                break;
            }
        }
    }

    return true;
}
 
Example 6
Source File: PaddingInfo.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Returns true if the parent component contains the child. (Does not return
 * true if the parent and child arguments are the same.)
 * 
 * @param parent
 * @param child
 * @return
 */
private static boolean contains(Component parent, Component child) {
	Component c = child;
	while (c != null) {
		c = c.getParent();

		if (parent == c) {
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: ProfilerTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void processFocusedComponent(Component c) {
    Component cc = c;
    while (c != null) {
        if (c == ProfilerTopComponent.this) {
            lastFocusOwner = cc;
            return;
        }
        c = c.getParent();
    }
}
 
Example 8
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param component The component whose ancestor chain is to be looked at.
 * @param type      The type of ancestor being looked for.
 * @return The ancestor, or {@code null}.
 */
@SuppressWarnings("unchecked")
public static <T> T getAncestorOfType(Component component, Class<T> type) {
    if (component == null) {
        return null;
    }
    Container parent = component.getParent();
    while (parent != null && !type.isAssignableFrom(parent.getClass())) {
        parent = parent.getParent();
    }
    return (T) parent;
}
 
Example 9
Source File: DefaultListCellRenderer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Overridden for performance reasons.
 * See the <a href="#override">Implementation Note</a>
 * for more information.
 *
 * @since 1.5
 * @return <code>true</code> if the background is completely opaque
 *         and differs from the JList's background;
 *         <code>false</code> otherwise
 */
@Override
public boolean isOpaque() {
    Color back = getBackground();
    Component p = getParent();
    if (p != null) {
        p = p.getParent();
    }
    // p should now be the JList.
    boolean colorMatch = (back != null) && (p != null) &&
        back.equals(p.getBackground()) &&
                    p.isOpaque();
    return !colorMatch && super.isOpaque();
}
 
Example 10
Source File: MetalBorders.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
    if (!(c instanceof JMenuItem)) {
        return;
    }
    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();

    g.translate( x, y );

    if ( c.getParent() instanceof JMenuBar ) {
        if ( model.isArmed() || model.isSelected() ) {
            g.setColor( MetalLookAndFeel.getControlDarkShadow() );
            g.drawLine( 0, 0, w - 2, 0 );
            g.drawLine( 0, 0, 0, h - 1 );
            g.drawLine( w - 2, 2, w - 2, h - 1 );

            g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
            g.drawLine( w - 1, 1, w - 1, h - 1 );

            g.setColor( MetalLookAndFeel.getMenuBackground() );
            g.drawLine( w - 1, 0, w - 1, 0 );
        }
    } else {
        if (  model.isArmed() || ( c instanceof JMenu && model.isSelected() ) ) {
            g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
            g.drawLine( 0, 0, w - 1, 0 );

            g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
            g.drawLine( 0, h - 1, w - 1, h - 1 );
        } else {
            g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
            g.drawLine( 0, 0, 0, h - 1 );
        }
    }

    g.translate( -x, -y );
}
 
Example 11
Source File: WindowUtils.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param comp The {@link Component} to use. May be {@code null}.
 * @return The most logical {@link Window} associated with the component.
 */
public static Window getWindowForComponent(Component comp) {
    while (true) {
        if (comp == null) {
            return JOptionPane.getRootFrame();
        }
        if (comp instanceof Frame || comp instanceof Dialog) {
            return (Window) comp;
        }
        comp = comp.getParent();
    }
}
 
Example 12
Source File: ExecutableInputMethodManager.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void notifyChangeRequestByHotKey(Component comp) {
    while (!(comp instanceof Frame || comp instanceof Dialog)) {
        if (comp == null) {
            // no Frame or Dialog found in containment hierarchy.
            return;
        }
        comp = comp.getParent();
    }

    notifyChangeRequest(comp);
}
 
Example 13
Source File: XComponentPeer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void removeDropTarget(DropTarget dt) {
    Component comp = target;
    while(!(comp == null || comp instanceof Window)) {
        comp = comp.getParent();
    }

    if (comp instanceof Window) {
        XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
        if (wpeer != null) {
            wpeer.removeDropTarget();
        }
    }
}
 
Example 14
Source File: CssExternalDropHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JEditorPane findPane(Component component) {
    while (component != null) {
        if (component instanceof JEditorPane) {
            return (JEditorPane) component;
        }
        component = component.getParent();
    }
    return null;
}
 
Example 15
Source File: BasicPopupMenuUI.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void stateChanged(ChangeEvent ev) {
    if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {
        uninstall();
        return;
    }
    MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
    MenuElement[] p = msm.getSelectedPath();
    JPopupMenu popup = getActivePopup(p);
    if (popup != null && !popup.isFocusable()) {
        // Do nothing for non-focusable popups
        return;
    }

    if (lastPathSelected.length != 0 && p.length != 0 ) {
        if (!checkInvokerEqual(p[0],lastPathSelected[0])) {
            removeItems();
            lastPathSelected = new MenuElement[0];
        }
    }

    if (lastPathSelected.length == 0 && p.length > 0) {
        // menu posted
        JComponent invoker;

        if (popup == null) {
            if (p.length == 2 && p[0] instanceof JMenuBar &&
                p[1] instanceof JMenu) {
                // a menu has been selected but not open
                invoker = (JComponent)p[1];
                popup = ((JMenu)invoker).getPopupMenu();
            } else {
                return;
            }
        } else {
            Component c = popup.getInvoker();
            if(c instanceof JFrame) {
                invoker = ((JFrame)c).getRootPane();
            } else if(c instanceof JDialog) {
                invoker = ((JDialog)c).getRootPane();
            } else if(c instanceof JApplet) {
                invoker = ((JApplet)c).getRootPane();
            } else {
                while (!(c instanceof JComponent)) {
                    if (c == null) {
                        return;
                    }
                    c = c.getParent();
                }
                invoker = (JComponent)c;
            }
        }

        // remember current focus owner
        lastFocused = KeyboardFocusManager.
            getCurrentKeyboardFocusManager().getFocusOwner();

        // request focus on root pane and install keybindings
        // used for menu navigation
        invokerRootPane = SwingUtilities.getRootPane(invoker);
        if (invokerRootPane != null) {
            invokerRootPane.addFocusListener(rootPaneFocusListener);
            invokerRootPane.requestFocus(true);
            invokerRootPane.addKeyListener(this);
            focusTraversalKeysEnabled = invokerRootPane.
                              getFocusTraversalKeysEnabled();
            invokerRootPane.setFocusTraversalKeysEnabled(false);

            menuInputMap = getInputMap(popup, invokerRootPane);
            addUIInputMap(invokerRootPane, menuInputMap);
            addUIActionMap(invokerRootPane, menuActionMap);
        }
    } else if (lastPathSelected.length != 0 && p.length == 0) {
        // menu hidden -- return focus to where it had been before
        // and uninstall menu keybindings
           removeItems();
    } else {
        if (popup != lastPopup) {
            receivedKeyPressed = false;
        }
    }

    // Remember the last path selected
    lastPathSelected = p;
    lastPopup = popup;
}
 
Example 16
Source File: XInputMethod.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Container getParent(Component client) {
    return client.getParent();
}
 
Example 17
Source File: BasicPopupMenuUI.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void stateChanged(ChangeEvent ev) {
    if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {
        uninstall();
        return;
    }
    MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
    MenuElement[] p = msm.getSelectedPath();
    JPopupMenu popup = getActivePopup(p);
    if (popup != null && !popup.isFocusable()) {
        // Do nothing for non-focusable popups
        return;
    }

    if (lastPathSelected.length != 0 && p.length != 0 ) {
        if (!checkInvokerEqual(p[0],lastPathSelected[0])) {
            removeItems();
            lastPathSelected = new MenuElement[0];
        }
    }

    if (lastPathSelected.length == 0 && p.length > 0) {
        // menu posted
        JComponent invoker;

        if (popup == null) {
            if (p.length == 2 && p[0] instanceof JMenuBar &&
                p[1] instanceof JMenu) {
                // a menu has been selected but not open
                invoker = (JComponent)p[1];
                popup = ((JMenu)invoker).getPopupMenu();
            } else {
                return;
            }
        } else {
            Component c = popup.getInvoker();
            if(c instanceof JFrame) {
                invoker = ((JFrame)c).getRootPane();
            } else if(c instanceof JDialog) {
                invoker = ((JDialog)c).getRootPane();
            } else if(c instanceof JApplet) {
                invoker = ((JApplet)c).getRootPane();
            } else {
                while (!(c instanceof JComponent)) {
                    if (c == null) {
                        return;
                    }
                    c = c.getParent();
                }
                invoker = (JComponent)c;
            }
        }

        // remember current focus owner
        lastFocused = KeyboardFocusManager.
            getCurrentKeyboardFocusManager().getFocusOwner();

        // request focus on root pane and install keybindings
        // used for menu navigation
        invokerRootPane = SwingUtilities.getRootPane(invoker);
        if (invokerRootPane != null) {
            invokerRootPane.addFocusListener(rootPaneFocusListener);
            invokerRootPane.requestFocus(true);
            invokerRootPane.addKeyListener(this);
            focusTraversalKeysEnabled = invokerRootPane.
                              getFocusTraversalKeysEnabled();
            invokerRootPane.setFocusTraversalKeysEnabled(false);

            menuInputMap = getInputMap(popup, invokerRootPane);
            addUIInputMap(invokerRootPane, menuInputMap);
            addUIActionMap(invokerRootPane, menuActionMap);
        }
    } else if (lastPathSelected.length != 0 && p.length == 0) {
        // menu hidden -- return focus to where it had been before
        // and uninstall menu keybindings
           removeItems();
    } else {
        if (popup != lastPopup) {
            receivedKeyPressed = false;
        }
    }

    // Remember the last path selected
    lastPathSelected = p;
    lastPopup = popup;
}
 
Example 18
Source File: FlatButtonUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
static boolean isToolBarButton( Component c ) {
	return c.getParent() instanceof JToolBar ||
		(c instanceof AbstractButton && clientPropertyEquals( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON ));
}
 
Example 19
Source File: FlatDatePickerBorder.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isTableCellEditor( Component c ) {
	return c.getParent() instanceof JTable;
}
 
Example 20
Source File: JLayeredPane.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/** Convenience method that returns the first JLayeredPane which
  * contains the specified component. Note that all JFrames have a
  * JLayeredPane at their root, so any component in a JFrame will
  * have a JLayeredPane parent.
  *
  * @param c the Component to check
  * @return the JLayeredPane that contains the component, or
  *         null if no JLayeredPane is found in the component
  *         hierarchy
  * @see JFrame
  * @see JRootPane
  */
public static JLayeredPane getLayeredPaneAbove(Component c) {
    if(c == null) return null;

    Component parent = c.getParent();
    while(parent != null && !(parent instanceof JLayeredPane))
        parent = parent.getParent();
    return (JLayeredPane)parent;
}