Example usage for javax.swing SwingUtilities isDescendingFrom

List of usage examples for javax.swing SwingUtilities isDescendingFrom

Introduction

In this page you can find the example usage for javax.swing SwingUtilities isDescendingFrom.

Prototype

public static boolean isDescendingFrom(Component a, Component b) 

Source Link

Document

Return true if a component a descends from a component b

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox combo = new JComboBox();
    combo.setEditable(true);//from ww w. jav  a  2s.  com
    for (int i = 0; i < 10; i++) {
        combo.addItem(i);
    }
    JLabel tip = new JLabel();
    tip.setText("Outside combobox");
    JPanel panel = new JPanel();
    panel.add(combo);
    panel.add(tip);
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            tip.setText("Outside combobox");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
            tip.setText(c != null && SwingUtilities.isDescendingFrom(c, combo) ? "Inside combo box"
                    : "Outside combobox");
        }
    });
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Use appropriately {@link SwingUtilities#isDescendingFrom(Component, Component)} which is strictly equivalent
 * //from  w  ww .j av  a 2s  .  c  o  m
 * @param c
 * @param container
 * @return
 */
@Deprecated
public static boolean isComponentContainedInContainer(Component c, Container container) {
    return SwingUtilities.isDescendingFrom(c, container);
}

From source file:Main.java

/**
 * <p>/*  w ww  .  j  a  v  a  2 s . c  o  m*/
 * Determines if the component is visible in its window at the given screen location.
 * </p>
 *
 * @param    location   A location on the screen.
 * @param    component   A component in a window.
 * @return            True if the component is visible in its window at the given screen location.
 */
public static boolean locationInComponentVisible(Point location, Component component) {

    // Get the root component in the window.
    JRootPane rootPane = getRootPane(component);
    if (rootPane != null) {
        Component rootComponent = rootPane.getContentPane();
        if (rootComponent != null) {
            // Get the location relative to this root component.
            Point locationInRoot = new Point(location);
            SwingUtilities.convertPointFromScreen(locationInRoot, rootComponent);

            // Get the deepest visible component at the given location.
            Component deepestComponent = SwingUtilities.getDeepestComponentAt(rootComponent, locationInRoot.x,
                    locationInRoot.y);
            if (deepestComponent != null) {
                boolean result = SwingUtilities.isDescendingFrom(deepestComponent, component);
                return result;
            }
        }
    }

    return false;

}

From source file:Main.java

public Main(final Frame owner) {
    super(owner);
    JPanel pnl = new JPanel(new BorderLayout());
    pnl.add(new JLabel("Click outside this dialog in the parent frame to close it"), BorderLayout.NORTH);
    JButton btn = new JButton("Click Me");
    btn.addActionListener(e -> JOptionPane.showMessageDialog(Main.this, "New Child Window"));
    pnl.add(btn, BorderLayout.CENTER);
    this.setContentPane(pnl);
    this.pack();//from   w ww .j a  va  2 s  .co m
    this.setLocationRelativeTo(owner);
    this.setAlwaysOnTop(true);
    this.addWindowFocusListener(new WindowFocusListener() {
        public void windowGainedFocus(WindowEvent e) {
        }

        public void windowLostFocus(WindowEvent e) {
            if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), Main.this)) {
                return;
            }
            Main.this.setVisible(false);
        }
    });
}

From source file:Main.java

public static Component compositeRequestFocus(Component component) {
    if (component instanceof Container) {
        Container container = (Container) component;
        if (container.isFocusCycleRoot()) {
            FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
            Component comp = policy.getDefaultComponent(container);
            if (comp != null) {
                comp.requestFocus();//from   ww  w  .ja  v a2  s .com
                return comp;
            }
        }
        Container rootAncestor = container.getFocusCycleRootAncestor();
        if (rootAncestor != null) {
            FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
            Component comp = policy.getComponentAfter(rootAncestor, container);

            if (comp != null && SwingUtilities.isDescendingFrom(comp, container)) {
                comp.requestFocus();
                return comp;
            }
        }
    }
    if (component.isFocusable()) {
        component.requestFocus();
        return component;
    }
    return null;
}

From source file:org.freeplane.main.application.MapViewDockingWindows.java

public MapViewDockingWindows() {
    viewSerializer = new MapViewSerializer();
    rootWindow = new RootWindow(viewSerializer);
    RootWindowProperties rootWindowProperties = rootWindow.getRootWindowProperties();
    rootWindowProperties.addSuperObject(new BlueHighlightDockingTheme().getRootWindowProperties());
    rootWindowProperties.getWindowAreaProperties().setBackgroundColor(UIManager.getColor("Panel.background"));
    rootWindow.getWindowBar(Direction.DOWN).setEnabled(true);
    try {/*from   ww w . ja v a 2s. com*/
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream wrapper = new ObjectOutputStream(byteStream);
        rootWindow.write(wrapper);
        wrapper.close();
        emptyConfigurations = byteStream.toByteArray();
    } catch (IOException e1) {
    }
    removeDesktopPaneAccelerators();
    mapViews = new Vector<Component>();
    final FileOpener fileOpener = new FileOpener();
    new DropTarget(rootWindow, fileOpener);
    rootWindow.addMouseListener(new DefaultMapMouseListener());

    final Controller controller = Controller.getCurrentController();
    controller.getMapViewManager().addMapViewChangeListener(this);
    rootWindow.addListener(new DockingWindowAdapter() {

        @Override
        public void viewFocusChanged(View previouslyFocusedView, View focusedView) {
            if (previouslyFocusedView != null && focusedView != null) {
                Component containedMapView = getContainedMapView(focusedView);
                viewSelectionChanged(containedMapView);
            }
        }

        @Override
        public void windowClosing(DockingWindow window) throws OperationAbortedException {
            for (Component mapViewComponent : mapViews.toArray(new Component[] {}))
                if (SwingUtilities.isDescendingFrom(mapViewComponent, window))
                    if (!Controller.getCurrentController().getMapViewManager().close(mapViewComponent, false))
                        throw new OperationAbortedException("can not close view");
        }

        @Override
        public void windowAdded(final DockingWindow addedToWindow, final DockingWindow addedWindow) {
            if (addedWindow instanceof TabWindow) {
                final DockingWindowProperties windowProperties = addedWindow.getWindowProperties();
                windowProperties.setDockEnabled(false);
                windowProperties.setUndockEnabled(false);
                final TabAreaProperties tabAreaProperties = ((TabWindow) addedWindow).getTabWindowProperties()
                        .getTabbedPanelProperties().getTabAreaProperties();
                if (addedToWindow == rootWindow)
                    tabAreaProperties.setTabAreaVisiblePolicy(TabAreaVisiblePolicy.MORE_THAN_ONE_TAB);
                else
                    tabAreaProperties.setTabAreaVisiblePolicy(TabAreaVisiblePolicy.ALWAYS);
            }
            setTabPolicies(addedWindow);
        }

        private void setTabPolicies(final DockingWindow window) {
            if (window instanceof TabWindow) {
                TabbedPanelProperties tabbedPanelProperties = ((TabWindow) window).getTabWindowProperties()
                        .getTabbedPanelProperties();
                if (!tabbedPanelProperties.getTabLayoutPolicy().equals(TabLayoutPolicy.COMPRESSION))
                    tabbedPanelProperties.setTabLayoutPolicy(TabLayoutPolicy.COMPRESSION);
                if (!tabbedPanelProperties.getTabDropDownListVisiblePolicy()
                        .equals(TabDropDownListVisiblePolicy.MORE_THAN_ONE_TAB))
                    tabbedPanelProperties
                            .setTabDropDownListVisiblePolicy(TabDropDownListVisiblePolicy.MORE_THAN_ONE_TAB);
            }
            for (int i = 0; i < window.getChildWindowCount(); i++) {
                setTabPolicies(window.getChildWindow(i));
            }
        }

        @Override
        public void windowRemoved(DockingWindow removedFromWindow, DockingWindow removedWindow) {
            if (removedWindow instanceof TabWindow) {
                if (removedFromWindow == rootWindow) {
                    final TabAreaProperties tabAreaProperties = ((TabWindow) removedWindow)
                            .getTabWindowProperties().getTabbedPanelProperties().getTabAreaProperties();
                    tabAreaProperties.setTabAreaVisiblePolicy(TabAreaVisiblePolicy.ALWAYS);
                }
            }
        }
    });

    new InternalFrameAdapter() {
        @Override
        public void internalFrameClosing(InternalFrameEvent e) {
        }
    };

}