java.awt.Component#getParent ( )源码实例Demo

下面列出了java.awt.Component#getParent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-8   文件: GroupLayout.java
/**
 * 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();
}
 
源代码2 项目: rapidminer-studio   文件: RoundedRectanglePopup.java
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());
		}
	}
}
 
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;
}
 
源代码4 项目: jdk8u_jdk   文件: AncestorNotifier.java
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;
    }
}
 
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;
}
 
源代码6 项目: netbeans   文件: CssExternalDropHandler.java
private JEditorPane findPane(Component component) {
    while (component != null) {
        if (component instanceof JEditorPane) {
            return (JEditorPane) component;
        }
        component = component.getParent();
    }
    return null;
}
 
源代码7 项目: pumpernickel   文件: PaddingInfo.java
/**
 * 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;
}
 
源代码8 项目: jdk8u_jdk   文件: XComponentPeer.java
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();
        }
    }
}
 
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);
}
 
源代码10 项目: gcs   文件: WindowUtils.java
/**
 * @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();
    }
}
 
源代码11 项目: hottub   文件: MetalBorders.java
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 );
}
 
源代码12 项目: dragonwell8_jdk   文件: DefaultListCellRenderer.java
/**
 * 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();
}
 
源代码13 项目: gcs   文件: UIUtilities.java
/**
 * @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;
}
 
源代码14 项目: netbeans   文件: ProfilerTopComponent.java
private void processFocusedComponent(Component c) {
    Component cc = c;
    while (c != null) {
        if (c == ProfilerTopComponent.this) {
            lastFocusOwner = cc;
            return;
        }
        c = c.getParent();
    }
}
 
源代码15 项目: FlatLaf   文件: FlatButtonUI.java
static boolean isToolBarButton( Component c ) {
	return c.getParent() instanceof JToolBar ||
		(c instanceof AbstractButton && clientPropertyEquals( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON ));
}
 
源代码16 项目: jdk8u-jdk   文件: XInputMethod.java
protected Container getParent(Component client) {
    return client.getParent();
}
 
源代码17 项目: jdk8u_jdk   文件: BasicPopupMenuUI.java
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;
}
 
源代码18 项目: jdk8u-dev-jdk   文件: BasicPopupMenuUI.java
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;
}
 
源代码19 项目: FlatLaf   文件: FlatDatePickerBorder.java
@Override
protected boolean isTableCellEditor( Component c ) {
	return c.getParent() instanceof JTable;
}
 
源代码20 项目: JDKSourceCode1.8   文件: JLayeredPane.java
/** 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;
}