Example usage for java.awt Component getName

List of usage examples for java.awt Component getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Gets the name of the component.

Usage

From source file:FocusTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent e) {
            dumpInfo(e);//from   w  ww.  java2  s .c o m
        }

        public void focusLost(FocusEvent e) {
            dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };

    // First
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.setName("First");
    text.addFocusListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.NORTH);

    // Second
    panel = new JPanel();
    label = new JLabel("Label 2: ");
    text = new JTextField("14.0", 10);
    text.setName("Second");
    text.addFocusListener(listener);
    text.setHorizontalAlignment(JTextField.RIGHT);
    label.setDisplayedMnemonic(KeyEvent.VK_2);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static Component getDescendantWithName(Component c, String name) {
    if (c.getName() != null && c.getName().equals(name))
        return c;
    if (c instanceof Container) {
        for (int i = 0; i < ((Container) c).getComponentCount(); i++) {
            Component out = getDescendantWithName(((Container) c).getComponent(i), name);
            if (out != null)
                return out;
        }// www .j a v  a 2s  . c  o m
        return null;
    } else
        return null;
}

From source file:Main.java

public static Component findComponentByName(Component rootComponent, String name) {
    if (rootComponent.getName() != null)
        if (rootComponent.getName().equals(name))
            return rootComponent;
    if (rootComponent instanceof Container) {
        Component[] components = ((Container) rootComponent).getComponents();
        for (int i = 0; i < components.length; ++i) {
            Component comp = findComponentByName(components[i], name);
            if (comp != null)
                return comp;
        }//from   www  . j av a  2s.  com
    }
    return null;
}

From source file:Main.java

public static List<Component> findComponentsNameStartsWith(Container rootComponent, String startsWith,
        List<Component> res) {
    if (rootComponent instanceof Container) {
        Component[] components = ((Container) rootComponent).getComponents();
        for (Component comp : components) {
            String name = comp.getName();
            if (name != null && name.startsWith(startsWith))
                res.add(comp);//from   w  w  w.j a v a  2  s  .com
            if (comp instanceof Container)
                findComponentsNameStartsWith((Container) comp, startsWith, res);
        }
    }
    return res;
}

From source file:Main.java

static Map<String, Component> getComponents(Container container) {

    Map<String, Component> listComponent = Collections.EMPTY_MAP;

    if (container.getComponentCount() > 0) {
        listComponent = new HashMap<>();

        for (Component component : container.getComponents()) {
            if (component.getName() != null) {
                if (component instanceof JScrollPane) {
                    listComponent.putAll(getComponents(((JScrollPane) component).getViewport()));
                } else {
                    listComponent.put(component.getName(), component);
                }/*from  w w  w .j ava 2 s.c om*/
            }
        }

    }

    return listComponent;
}

From source file:Main.java

private static void printChildHierarchy(Component parent, int level) {
    StringBuilder prefix = new StringBuilder("");
    for (int i = 0; i <= level; i++) {
        prefix.append(" ");
    }//from  ww  w.java 2  s.c  o  m
    if (parent instanceof Container) {
        Component[] children = (parent instanceof JMenu) ? ((JMenu) parent).getMenuComponents()
                : ((Container) parent).getComponents();

        for (int i = 0; i < children.length; ++i) {
            Component child = children[i];
            System.out.println(prefix + " " + child.getName()
                    + (child instanceof AbstractButton ? " A:" + ((AbstractButton) child).getActionCommand()
                            : "")
                    + " = " + child);
            printChildHierarchy(child, level + 1);
        }
    }
}

From source file:Main.java

public static <T> T findChildByName(Container container, Class<T> returnType, String name) {

    if (name.equals(container.getName())) {
        return returnType.cast(container);
    }/*from www .  j a  v  a 2s.c  om*/

    for (Component component : container.getComponents()) {
        if (name.equals(component.getName())) {
            return returnType.cast(component);
        } else if (component instanceof Container) {
            T recursiveResult = findChildByName((Container) component, returnType, name);

            if (recursiveResult != null) {
                return returnType.cast(recursiveResult);
            }
        }
    }

    throw new IllegalArgumentException(
            "Did not find child component by name [" + name + "] in the specified container!");
}

From source file:Main.java

public static Component getChild(Component parent, String name) {
    parent = getContainer(parent);/*w ww.  ja  v a 2s.  c  om*/

    if (parent instanceof JSplitPane) {
        JSplitPane split = (JSplitPane) parent;
        if (JSplitPane.TOP.equals(name)) {
            return split.getTopComponent();
        } else if (JSplitPane.LEFT.equals(name)) {
            return split.getLeftComponent();
        } else if (JSplitPane.RIGHT.equals(name)) {
            return split.getRightComponent();
        } else if (JSplitPane.BOTTOM.equals(name)) {
            return split.getBottomComponent();
        }
    }
    Container cont = (Container) parent;
    for (int i = 0; i < cont.getComponentCount(); i++) {
        Component comp = cont.getComponent(i);
        if (name.equals(comp.getName())) {
            return comp;
        }
    }
    if (name.endsWith(VIEW_SUFFIX)) {
        String subName = name.substring(0, name.length() - VIEW_SUFFIX.length());
        if (subName.isEmpty()) {
            return parent;
        }
        return getContainer(getChild(parent, subName));
    }

    throw new IllegalArgumentException("No component named " + name);
}

From source file:Main.java

public static <T extends Component> T search(String name, Component comp) {
    if (name.equals(comp.getName())) {
        return (T) comp;
    } else if (comp instanceof Container) {
        for (Component c : ((Container) comp).getComponents())
            if ((c = search(name, c)) != null)
                return (T) c;
    }/*www .j ava  2 s. c  om*/
    return null;
}

From source file:Main.java

private static void searchFor(Container c, Class cls, List list, String name) {
    if (!isEmpty(c.getComponents())) {
        for (Component comp : c.getComponents()) {
            if (cls.isInstance(comp)
                    && (name == null ? true : (comp.getName() != null && comp.getName().matches(name)))) {
                list.add(comp);//ww  w  . jav  a2s  . com
            } else if (comp instanceof Container) {
                searchFor((Container) comp, cls, list, name);
            }
        }
    }
}