Example usage for java.awt Component isShowing

List of usage examples for java.awt Component isShowing

Introduction

In this page you can find the example usage for java.awt Component isShowing.

Prototype

public boolean isShowing() 

Source Link

Document

Determines whether this component is showing on screen.

Usage

From source file:ComponentTree.java

/**
 * This main() method demonstrates the use of the ComponentTree class: it
 * puts a ComponentTree component in a Frame, and uses the ComponentTree to
 * display its own GUI hierarchy. It also adds a TreeSelectionListener to
 * display additional information about each component as it is selected
 *///from   ww w.ja v a  2s .  com
public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            // Tree selections are referred to by "path"
            // We only care about the last node in the path
            TreePath path = e.getPath();
            Component c = (Component) path.getLastPathComponent();
            // Now we know what component was selected, so
            // display some information about it in the message line
            if (c.isShowing()) {
                Point p = c.getLocationOnScreen();
                msgline.setText("x: " + p.x + "  y: " + p.y + "  width: " + c.getWidth() + "  height: "
                        + c.getHeight());
            } else {
                msgline.setText("component is not showing");
            }
        }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Returns the screen location for a component and X and Y.
 *///from w ww.  j a  v a 2 s.  c  o  m
private static Point getScreenLocation(Component aComp, int anX, int aY) {
    Point point = aComp != null && aComp.isShowing() ? aComp.getLocationOnScreen() : new Point();
    point.x += anX;
    point.y += aY;
    return point;
}

From source file:Main.java

public static Point getRelLocation(Component c) {
    if (c == null || !c.isShowing()) {
        return new Point(0, 0);
    }/*from  w  w w  .ja v  a2s .  co  m*/

    Container parent = getRootContainer(c);
    if ((parent != null) && parent.isShowing()) {
        Point p1 = c.getLocationOnScreen();
        Point p2 = parent.getLocationOnScreen();
        return new Point(p1.x - p2.x, p1.y - p2.y);
    }

    return new Point(0, 0);
}

From source file:Main.java

public static Component findComponentUnderGlassPaneAt(Point p, Component top) {
    Component c = null;/*w  w  w. j a v  a 2  s . c om*/

    if (top.isShowing()) {
        if (top instanceof RootPaneContainer)
            c = ((RootPaneContainer) top).getLayeredPane().findComponentAt(
                    SwingUtilities.convertPoint(top, p, ((RootPaneContainer) top).getLayeredPane()));
        else
            c = ((Container) top).findComponentAt(p);
    }

    return c;
}

From source file:Main.java

public static List<JComponent> getComponents(Container owner, Class<?> clazz, boolean onlyVisible) {
    List<JComponent> list = new ArrayList<JComponent>();
    for (Component c : owner.getComponents()) {
        if (clazz.isInstance(c) && (!onlyVisible || c.isShowing()))
            list.add((JComponent) c);
        else if (c instanceof JComponent) {
            for (JComponent b : getComponents((JComponent) c, clazz, onlyVisible))
                list.add(b);/*from   w ww.  j  av a2 s.  c  o m*/
        }
    }
    return list;
}

From source file:eu.delving.sip.base.VisualFeedback.java

private static void acquireFocus() {
    Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    if (fo != null && fo.isShowing())
        fo.requestFocus();/*  ww  w . j  a  v a2s.c  o m*/
}

From source file:AWTUtilities.java

/**
 * Packs and centers the given window relative to the given component. The
 * specified component may be <code>null</code>, in which case the window will
 * be centered on the screen. The method also makes sure that the target
 * window is fully visible by calling <code>forceToScreen</code>.
 *///w w w  .j  a  v a  2 s.c  o  m

public static void centerWindow(Window target, Component parent) {
    target.pack();

    Dimension size = target.getSize();
    Rectangle parentBounds = parent == null || !parent.isShowing() ? getUsableScreenBounds()
            : new Rectangle(parent.getLocationOnScreen(), parent.getSize());

    target.setLocation(parentBounds.x + (parentBounds.width - size.width) / 2,
            parentBounds.y + (parentBounds.height - size.height) / 2);

    forceToScreen(target);
}

From source file:org.eclipse.jubula.rc.swing.components.AUTSwingHierarchy.java

/**
 * Searchs for the component in the AUT with the given
 * <code>componentIdentifier</code>.
 * @param componentIdentifier the identifier created in object mapping mode
 * @throws IllegalArgumentException if the given identifer is null or <br>the hierarchy is not valid: empty or containing null elements
 * @throws InvalidDataException if the hierarchy in the componentIdentifier does not consist of strings
 * @throws ComponentNotManagedException if no component could be found for the identifier
 * @return the instance of the component of the AUT 
 *//*from w w w .  j  a  v a 2 s .co  m*/
public Component findComponent(IComponentIdentifier componentIdentifier)
        throws IllegalArgumentException, ComponentNotManagedException, InvalidDataException {
    Component comp = (Component) findBP.findComponent(componentIdentifier, ComponentHandler.getAutHierarchy());

    if (comp != null && comp.isShowing()) {
        Window window = SwingUtilities.getWindowAncestor(comp);
        if (window != null && window.isShowing() && !window.isActive()) {
            window.toFront();
        }
        return comp;
    }
    throw new ComponentNotManagedException("unmanaged component with identifier: '" //$NON-NLS-1$
            + componentIdentifier.toString() + "'.", //$NON-NLS-1$ 
            MessageIDs.E_COMPONENT_NOT_MANAGED);
}

From source file:org.eclipse.jubula.rc.swing.components.AUTSwingHierarchy.java

/**
 * Add the new component to the hierarchy.
 * /*from   w w w. ja  v a2  s. c om*/
 * @param toAdd The component to add to the hierarchy.
 */
private void addComponent(Component toAdd) {
    ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    try {
        // Don't add an invisible component
        if (!toAdd.isShowing()) {
            return;
        }
        Container container = toAdd.getParent();
        if (log.isDebugEnabled()) {
            log.debug("component '" + toAdd //$NON-NLS-1$
                    + "' added to '" + container + "'"); //$NON-NLS-1$ //$NON-NLS-2$                  
        }
        // get the hierarchy container for container, must be there!
        SwingHierarchyContainer hierarchyContainer = null;
        if (toAdd instanceof Window) {
            hierarchyContainer = getHierarchyContainer(toAdd);
        } else {
            hierarchyContainer = getHierarchyContainer(container);
        }

        if (hierarchyContainer == null) {
            // Parent container not managed at this time.
            // Do not clutter up the hierarchy with orphan components.
            return;
        }
        // create new hierarchy container for child, name, update hashtable, put
        // them together,
        if (getHierarchyContainer(toAdd) != null) {
            return;
        }
        // create new hierarchy container for child, name, update hashtable, put
        // them together,
        SwingHierarchyContainer hierarchyChild = new SwingHierarchyContainer(new SwingComponent(toAdd));
        hierarchyContainer.add(hierarchyChild);
        hierarchyChild.setParent(hierarchyContainer);
        name(hierarchyChild);
        addToHierachyMap(hierarchyChild);
        if (toAdd instanceof Container) {
            Container cont = (Container) toAdd;
            // call addTohierachyDown()
            addToHierarchyDown(hierarchyChild, cont);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(originalCL);
    }
}

From source file:org.eclipse.jubula.rc.swing.components.AUTSwingHierarchy.java

/**
 * adds the children of the given container to the hierachy.
 * @param hierarchyContainer the responding container (meta data)
 * @param container the container from the AUT, which childrens are to be added
 *///from  www . j a va  2 s .c om
private void addToHierarchyDown(SwingHierarchyContainer hierarchyContainer, Container container) {

    checkDispatchThread();
    if (log.isInfoEnabled()) {
        log.info("addToHierarchyDown: " + hierarchyContainer + "," + container); //$NON-NLS-1$ //$NON-NLS-2$
    }
    registerAsContainerListener(container);
    Collection collection = getComponents(container);
    for (Iterator iter = collection.iterator(); iter.hasNext();) {
        final Component comp = (Component) iter.next();

        // Don't add if the component is already in our hierarchy or 
        // if it is invisible.
        if (getHierarchyContainer(comp) != null || !comp.isShowing()) {
            continue;
        }

        if (comp instanceof Window) {
            add((Window) comp);
        } else {
            // add the container
            SwingHierarchyContainer newHierarchyContainer = new SwingHierarchyContainer(
                    new SwingComponent(comp));
            name(newHierarchyContainer);
            // update the hash table
            newHierarchyContainer.setParent(hierarchyContainer);
            hierarchyContainer.add(newHierarchyContainer);
            addToHierachyMap(newHierarchyContainer);
            if (comp instanceof Container) {
                // recursivly down
                addToHierarchyDown(newHierarchyContainer, (Container) comp);
            }
        }
    }
    name(hierarchyContainer);
}