Java JComboBox getOnlyComboBox(Container owner)

Here you can find the source of getOnlyComboBox(Container owner)

Description

get Only Combo Box

License

Open Source License

Declaration

public static JComboBox<?> getOnlyComboBox(Container owner) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.ArrayList;

import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;

public class Main {
    public static JComboBox<?> getOnlyComboBox(Container owner) {
        return getOnlyComboBox(owner, false);
    }/*from w  w w  . j a v a 2  s. com*/

    public static JComboBox<?> getOnlyComboBox(Container owner, boolean onlyVisible) {
        return (JComboBox<?>) getOnlyComponent(owner, JComboBox.class, onlyVisible);
    }

    private static JComponent getOnlyComponent(Container owner, Class<?> clazz) {
        return getOnlyComponent(owner, clazz, false);
    }

    private static JComponent getOnlyComponent(Container owner, Class<?> clazz, boolean onlyVisible) {
        List<JComponent> list = getComponents(owner, clazz, onlyVisible);
        if (list.size() != 1)
            throw new IllegalStateException(
                    "num " + (onlyVisible ? "visible " : "") + "compounds found " + list.size());
        return list.get(0);
    }

    public static List<JComponent> getComponents(Container owner, Class<?> clazz) {
        return getComponents(owner, clazz, false);
    }

    public static List<JComponent> getComponents(Container owner, Class<?> clazz, boolean onlyVisible) {
        List<JComponent> list = new ArrayList<JComponent>();
        for (Component c : owner.getComponents()) {
            if (clazz.isInstance(c) && (!onlyVisible || c.isShowing()))
                list.add((JComponent) c);
            else if (c instanceof JComponent) {
                for (JComponent b : getComponents((JComponent) c, clazz, onlyVisible))
                    list.add(b);
            }
        }
        return list;
    }
}

Related

  1. getAgeComboBox()
  2. getComboBoxDisabledBackground()
  3. getEOL(JComboBox eolCombo)
  4. getJComboBoxString(JComboBox comboBox)
  5. getMaxWidth(final JComboBox combo, final FontMetrics fm)
  6. getPrinterJComboBox()
  7. getScrollBarWidth(JComboBox comboBox, JScrollPane scrollPane)
  8. initCombo(JComboBox combo)
  9. inputComboBox(JComboBox cbx, String[] str)