Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Pablo Pavon Mario.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Pablo Pavon Mario - initial API and implementation
 ******************************************************************************/

import javax.swing.*;
import java.awt.*;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Enables/disables a {@code JComponent} and all its associated children.
     *
     * @param component {@code JComponent}
     * @param enabled   true if this component should be enabled, false otherwise
     * @since 0.3.0
     */
    public static void setEnabled(JComponent component, boolean enabled) {
        component.setEnabled(enabled);

        List<Component> children = getAllComponents(component);
        for (Component child : children)
            if (child instanceof JComponent)
                ((JComponent) child).setEnabled(enabled);
    }

    /**
     * Returns all the components (and their children) associated to the given container.
     *
     * @param container Container
     * @return {@code Component} list
     * @since 0.3.0
     */
    public static List<Component> getAllComponents(Container container) {
        List<Component> out = new LinkedList<Component>();
        for (Component comp : container.getComponents()) {
            out.add(comp);
            if (comp instanceof Container)
                out.addAll(getAllComponents((Container) comp));
        }

        return out;
    }
}