Example usage for java.awt Container getComponents

List of usage examples for java.awt Container getComponents

Introduction

In this page you can find the example usage for java.awt Container getComponents.

Prototype

public Component[] getComponents() 

Source Link

Document

Gets all the components in this container.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button);//from   www  .  j  ava  2s .co m
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(3, 3));

    for (int i = 9; i > 0; i--) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button, 0);//w  w  w. ja va 2s  .  c o m
    }

    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };

    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);

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

From source file:Main.java

private static void removeButton(Container container) {
    Component[] components = container.getComponents();
    for (Component component : components) {
        if (component instanceof AbstractButton) {
            container.remove(component);
        }//w  w w  .j av a2 s.  c  o m
    }
}

From source file:Main.java

private static void inactivateOption(Container container, String text) {
    Component[] comps = container.getComponents();
    for (Component comp : comps) {
        if (comp instanceof AbstractButton) {
            AbstractButton btn = (AbstractButton) comp;
            if (btn.getActionCommand().equals(text)) {
                btn.setEnabled(false);//from   w ww .  j a v a  2  s . c  o m
                return;
            }
        } else if (comp instanceof Container) {
            inactivateOption((Container) comp, text);
        }
    }

}

From source file:Main.java

public static void hideComponents(Container container) {
    for (Component component : container.getComponents()) {
        if (component instanceof Container) {
            component.setVisible(false);
        }/*from w ww.  j  a  v  a 2s  .c  o  m*/
    }
}

From source file:Main.java

public static <T extends Component> void addChildren(Class<T> clazz, Collection<? super T> dst,
        Container parent) {
    for (Component c : parent.getComponents()) {
        if (clazz.isInstance(c))
            dst.add(clazz.cast(c));/*from  w  w  w  .  java2  s .c  o m*/
    }
}

From source file:Main.java

public static void setEnableContainer(Container c, boolean band) {
    Component[] components = c.getComponents();
    c.setEnabled(band);//w w w .  j a  v  a2 s  .c  o  m
    for (int i = 0; i < components.length; i++) {
        components[i].setEnabled(band);

        if (components[i] instanceof Container) {
            setEnableContainer((Container) components[i], band);
        }
    }
}

From source file:Main.java

public static Component getComponentAt(Container parent, Point p) {
    Component comp = null;/*from  w  ww  .  j av a 2s  .  c o m*/
    for (Component child : parent.getComponents()) {
        if (child.getBounds().contains(p)) {
            comp = child;
        }
    }
    return comp;
}

From source file:Main.java

public static List<Component> getAllComponents(final Container c) {
    Component[] comps = c.getComponents();
    List<Component> compList = new ArrayList<Component>();
    for (Component comp : comps) {
        compList.add(comp);/*from   ww  w  .jav  a 2  s . com*/
        if (comp instanceof Container)
            compList.addAll(getAllComponents((Container) comp));
    }
    return compList;
}

From source file:Main.java

public static java.util.List<Component> getAllComponents(final Container container) {
    Component[] comps = container.getComponents();
    java.util.List<Component> compList = new ArrayList<>();
    for (Component comp : comps) {
        compList.add(comp);/*  w  w  w.  j  a v a  2 s .c o  m*/
        if (comp instanceof Container) {
            compList.addAll(getAllComponents((Container) comp));
        }
    }
    return compList;
}