Java JComponent Container checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow)

Here you can find the source of checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow)

Description

Check the components.

License

Apache License

Parameter

Parameter Description
_rootComponent the root component
_print whether to print the result or not
_currentColumn the current column number
_currentRow the current row number

Return

the total amount of rows and columns of the current graphical-user-interface-configuration

Declaration

private static Rectangle checkComponents(final Component _rootComponent, final String _print,
        final int _currentColumn, final int _currentRow) 

Method Source Code

//package com.java2s;
/*/*from   ww w .java 2  s  .c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Component;

import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    /**
     * Check the components.
     * Recursive method which returns the total amount of rows and columns.
     * 
     * @param _rootComponent    the root component
     * @param _print         whether to print the result or not
     * @param _currentColumn   the current column number
     * @param _currentRow      the current row number
     * @return               the total amount of rows and columns of the
     *                      current graphical-user-interface-configuration
     */
    private static Rectangle checkComponents(final Component _rootComponent, final String _print,
            final int _currentColumn, final int _currentRow) {

        //save given values.
        int depth = _currentColumn;
        int amount = _currentRow;
        int maxDepth = _currentColumn;

        if (_rootComponent.getWidth() <= 0 || _rootComponent.getHeight() <= 0) {
            System.err.println(_print + _rootComponent.getClass().getSimpleName() + _rootComponent.getSize());
        } else {
            System.out.println(_print + _rootComponent.getClass().getSimpleName()
            //               + _c.getSize() 
            );
        }

        if (_rootComponent instanceof JPanel) {
            for (Component x : ((JPanel) _rootComponent).getComponents()) {

                Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1);

                //update max depth and amount
                amount += pnt_recursiv.y;
                maxDepth = Math.max(maxDepth, pnt_recursiv.width);
            }

        } else if (_rootComponent instanceof JFrame) {

            for (Component x : ((JFrame) _rootComponent).getContentPane().getComponents()) {

                Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1);

                //update max depth
                amount += pnt_recursiv.y;
                maxDepth = Math.max(maxDepth, pnt_recursiv.width);
            }

        } else if (_rootComponent instanceof Panel) {

            for (Component x : ((Panel) _rootComponent).getComponents()) {

                Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1);

                //update max depth
                amount += pnt_recursiv.y;
                maxDepth = Math.max(maxDepth, pnt_recursiv.width);
            }

        } else if (_rootComponent instanceof Window) {

            for (Component x : ((Window) _rootComponent).getComponents()) {

                Rectangle pnt_recursiv = checkComponents(x, _print + "\t", depth + 1, 1);

                //update max depth
                amount += pnt_recursiv.y;
                maxDepth = Math.max(maxDepth, pnt_recursiv.width);
            }

        }

        Rectangle pnt_res = new Rectangle(depth, amount, maxDepth, 0);
        return pnt_res;
    }
}

Related

  1. allignBottom(JComponent lead, JComponent target)
  2. centerComponent(final JComponent target)
  3. centerOnComponet(Window target, JComponent parent)
  4. findMainRootPane(Component component)
  5. findRootPane(Component component)
  6. findRootPaneContainer(Component c)
  7. findRootPaneContainer(Component root)