Java JButton Settings findDescendantButtonWithText(Container root, String desiredText)

Here you can find the source of findDescendantButtonWithText(Container root, String desiredText)

Description

Find a descendant button in the container root that has a getText() method that returns the given text.

License

Open Source License

Parameter

Parameter Description
root the root of the hierarchy to search
desiredText the text of the button we're looking fore

Return

the descendant found, or null

Declaration

public static AbstractButton findDescendantButtonWithText(Container root, String desiredText) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
 * as represented by the Administrator of the National Aeronautics and Space 
 * Administration. All rights reserved./*from w  w  w  .j  a  v  a  2  s .  co  m*/
 *
 * The MCT platform is licensed 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.
 *
 * MCT includes source code licensed under additional open source licenses. See 
 * the MCT Open Source Licenses file included with this distribution or the About 
 * MCT Licenses dialog available at runtime from the MCT Help menu for additional 
 * information. 
 *******************************************************************************/

import java.awt.Component;
import java.awt.Container;

import javax.swing.AbstractButton;

public class Main {
    /**
     * Find a descendant button in the container <code>root</code> that has
     * a <code>getText()</code> method that returns the given text.
     *
     * @param root the root of the hierarchy to search
     * @param desiredText the text of the button we're looking fore
     * @return the descendant found, or null
     */
    public static AbstractButton findDescendantButtonWithText(Container root, String desiredText) {
        Component[] children = root.getComponents();

        // Try to find a direct child first.
        for (int i = 0; i < children.length; ++i) {
            if (children[i] instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) children[i];
                if (button.getText().equals(desiredText)) {
                    return button;
                }
            }
        }

        // OK, try child containers, too.
        for (int i = 0; i < children.length; ++i) {
            if (children[i] instanceof Container) {
                AbstractButton button = findDescendantButtonWithText((Container) children[i], desiredText);
                if (button != null) {
                    return button;
                }
            }
        }

        // Not found.
        return null;
    }
}

Related

  1. emphasizeButton(AbstractButton btn)
  2. equalizeButtonSizes(JPanel jPanelButtons)
  3. filterMnemonic(final String s, final AbstractButton b)
  4. findButton(Container container, String text)
  5. findButtonComponents(Container container, String label)
  6. fixButtonUI(AbstractButton button)
  7. formatButtonFromAction(Action a)
  8. getAbstractButton(Container owner, String text)
  9. getAllButtons(Component topComponent)