Adds the specified JComponent on the first Monitor ancestor of the specified Component. - Java Swing

Java examples for Swing:Window

Description

Adds the specified JComponent on the first Monitor ancestor of the specified Component.

Demo Code

/*//from  www .  jav a2s  .c o m
 * Copyright 2007-2013
 * Licensed under GNU Lesser General Public License
 * 
 * This file is part of EpochX
 * 
 * EpochX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * EpochX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with EpochX. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The latest version is available from: http://www.epochx.org
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;

public class Main{
    /**
     * Adds the specified <code>JComponent</code> on the first
     * <code>Monitor</code> ancestor of the specified <code>Component</code>.
     * 
     * @param c the <code>Component</code> whose parent <code>Monitor</code> is
     *        to be found to add the <code>JComponent</code>.
     * @paren toAdd the <code>JComponent</code> to add.
     * 
     * @return true if the parent <code>Monitor</code> is found and the
     *         <code>JComponent</code> is added.
     * 
     * @throws IllegalArgumentException if one of the arguments is null.
     */
    public static boolean addToTheParentMonitor(Component c,
            JComponent toAdd) throws IllegalArgumentException {

        if (c == null || toAdd == null) {
            throw new IllegalArgumentException("Null argument non permit.");
        }

        Monitor monitor = getMonitorAncestor(c);
        if (monitor != null) {
            monitor.add(toAdd);
            return true;
        } else {
            return false;
        }
    }
    /**
     * Returns the first <code>Monitor</code> ancestor of c, or null if c is not
     * contained inside a <code>Monitor</code>.
     * 
     * @param c the <code>Component</code> to get <code>Monitor</code> ancestor
     *        of.
     * 
     * @return the first <code>Monitor</code> ancestor of c, or null if c is not
     *         contained inside a <code>Monitor</code>.
     */
    public static Monitor getMonitorAncestor(Component c) {
        Component res = c;
        while (res != null && !(res instanceof Monitor)) {
            res = res.getParent();
        }
        return (Monitor) res;
    }
}

Related Tutorials