Example usage for java.awt Component getClass

List of usage examples for java.awt Component getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:UsingComponentListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox checkbox = new JCheckBox("Label visible", true);
    checkbox.addComponentListener(new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            System.out.println("componentHidden event from " + e.getComponent().getClass().getName());
        }/*from  w  w w  . j  av a  2s.c  o  m*/

        public void componentMoved(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentMoved event from " + c.getClass().getName() + "; new location: "
                    + c.getLocation().x + ", " + c.getLocation().y);
        }

        public void componentResized(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentResized event from " + c.getClass().getName() + "; new size: "
                    + c.getSize().width + ", " + c.getSize().height);
        }

        public void componentShown(ComponentEvent e) {
            System.out.println("componentShown event from " + e.getComponent().getClass().getName());
        }

    });

    frame.add(checkbox, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:ShowComponent.java

public static void main(String[] args) {
    // Process the command line to get the components to display
    Vector components = getComponentsFromArgs(args);

    // Create a frame (a window) to display them in
    JFrame frame = new JFrame("ShowComponent");

    // Handle window close requests by exiting the VM
    frame.addWindowListener(new WindowAdapter() { // Anonymous inner class
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from  w w  w . ja  va  2 s.  c o  m*/
        }
    });

    // Set up a menu system that allows the user to select the
    // look-and-feel of the component from a list of installed PLAFs
    JMenuBar menubar = new JMenuBar(); // Create a menubar
    frame.setJMenuBar(menubar); // Tell the frame to display it
    JMenu plafmenu = createPlafMenu(frame); // Create a menu
    menubar.add(plafmenu); // Add the menu to the menubar

    // Create a JTabbedPane to display each of the components
    JTabbedPane pane = new JTabbedPane();

    // Now add each component as a tab of the tabbed pane
    // Use the unqualified component classname as the tab text
    for (int i = 0; i < components.size(); i++) {
        Component c = (Component) components.elementAt(i);
        String classname = c.getClass().getName();
        String tabname = classname.substring(classname.lastIndexOf('.') + 1);
        pane.addTab(tabname, c);
    }

    // Add the tabbed pane to the frame. Note the call to getContentPane()
    // This is required for JFrame, but not for most Swing components
    frame.getContentPane().add(pane);

    // Set the frame size and pop it up
    frame.pack(); // Make frame as big as its kids need
    frame.setVisible(true); // Make the frame visible on the screen

    // The main() method exits now but the Java VM keeps running because
    // all AWT programs automatically start an event-handling thread.
}

From source file:Main.java

public static java.util.List<Component> getComponents(Container container, String classString) {
    java.util.List<Component> list = new ArrayList<>();
    for (Component component : getAllComponents(container)) {
        if (component.getClass().toString().endsWith(classString)) {
            list.add(component);//  w  ww .  j a v  a 2 s . c o m
        }
    }
    return list;
}

From source file:Main.java

public static List<Component> findChildComponentsOfType(Component component, Class<?> type) {
    List<Component> foundComponents = new ArrayList<Component>();
    if (component instanceof Container) {
        Container container = (Container) component;
        for (Component child : container.getComponents()) {
            if (type.isAssignableFrom(child.getClass())) {
                foundComponents.add(child);
            }/*  w w  w . j a  v  a  2s .c  o m*/
            foundComponents.addAll(findChildComponentsOfType(child, type));// recursive
        }
    }
    return foundComponents;
}

From source file:Main.java

public static String formateComponentInfosToPrint(Component comp) {
    StringBuilder buf = new StringBuilder();
    buf.append(comp.getClass().getSimpleName());
    buf.append(" [");
    buf.append(comp.getName());/*  w  ww  .j a  va  2s  .  c om*/
    if (comp instanceof JLabel)
        buf.append(",\"").append(((JLabel) comp).getText()).append("\"");
    buf.append(",");
    buf.append(comp.getClass().getName());
    buf.append(",");
    buf.append(comp.isVisible() ? "visible" : "not visible");
    buf.append(",");
    buf.append(comp.isValid() ? "valid" : "invalid");
    buf.append("]");
    return buf.toString();
}

From source file:Main.java

/**
 * @param aContainer/*w  ww .  ja v a  2 s  . co m*/
 * @param aComponentClass
 * @return
 */
private static Component findComponent(final Container aContainer,
        final Class<? extends Component> aComponentClass) {
    Component result = null;

    final int cnt = aContainer.getComponentCount();
    for (int i = 0; (result == null) && (i < cnt); i++) {
        final Component comp = aContainer.getComponent(i);
        if (aComponentClass.equals(comp.getClass())) {
            result = comp;
        } else if (comp instanceof Container) {
            result = findComponent((Container) comp, aComponentClass);
        }
    }
    return result;
}

From source file:Main.java

static public void printAllComponents(Component root, int level) {
    if (root == null)
        return;// www  . j a  v  a  2 s . c om

    printSpaceBeforeContent(level, root.getClass().getSimpleName());
    if (root instanceof Container) {
        Component[] components = ((Container) root).getComponents();
        if (components == null || components.length == 0) {
            return;
        }
        for (Component comp : components) {
            printAllComponents(comp, level + 1);
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
private static <T extends Component> void findChildComponentsRec(Component parent, Class<T> type, List<T> list,
        int maxCount) {
    if (type.isAssignableFrom(parent.getClass())) {
        list.add((T) parent);//from   www  . j  a v a2  s  .  com
        if (list.size() >= maxCount) {
            return;
        }
    }
    //
    if (parent instanceof Container) {
        for (Component c : ((Container) parent).getComponents()) {
            findChildComponentsRec(c, type, list, maxCount);
            if (list.size() >= maxCount) {
                return;
            }
        }
    }
}

From source file:Main.java

/**
 * @param fc/*from   w  ww. j a v a  2 s .  co  m*/
 * @param string
 */
private static void printComponentTree(final JComponent fc, final String string) {

    // c.setVisible(false);
    for (int i = 0; i < fc.getComponentCount(); i++) {
        final Component cc = fc.getComponent(i);
        System.out.println(string + "[" + i + "]" + cc.getClass().getSuperclass().getSimpleName() + ":" + cc
                + " Opaque: " + cc.isOpaque());

        if (cc instanceof JComponent) {
            printComponentTree((JComponent) cc, string + "[" + i + "]");
        }

    }
}

From source file:Main.java

public static <T extends Component> T findParentComponentOfType(Component component, Class<T> type) {
    do {//from w w  w  .j  a  va  2  s  . c o  m
        component = component.getParent();
        if (component != null && type.isAssignableFrom(component.getClass())) {
            return (T) component;
        }
    } while (component != null);

    return null;
}