Java JComboBox Item loadInstances(JComboBox target, Class source, Class limit, Object defaultItem)

Here you can find the source of loadInstances(JComboBox target, Class source, Class limit, Object defaultItem)

Description

load Instances

License

Open Source License

Declaration

@SuppressWarnings("unchecked") 
    public static <B> void loadInstances(JComboBox<B> target, Class<?> source, Class<?> limit, Object defaultItem) 

Method Source Code

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

import java.lang.reflect.InvocationTargetException;
import javax.swing.JComboBox;

public class Main {
    @SuppressWarnings("unchecked") //Several inherently not type-safe operation...
    public static <B> void loadInstances(JComboBox<B> target, Class<?> source, Class<?> limit, Object defaultItem) {
        Class<?>[] clss = source.getClasses();
        for (Class<?> cls : clss) {
            try {
                if (!limit.isAssignableFrom(cls)) {
                    continue;
                }//ww  w. j a  v a2  s .  c om
                if (cls.isInterface()) {
                    continue;
                }

                try {
                    B i = (B) cls.getConstructor().newInstance();
                    target.addItem(i);
                } catch (NoSuchMethodException e) {
                    continue;
                }
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | SecurityException e) {
                System.err.println("Error intializing GUI:" + cls.getName());
                e.printStackTrace();
            }
        }

        if (defaultItem != null) {
            for (int i = 0; i < target.getItemCount(); i++) {
                B item = target.getItemAt(i);
                if (item.toString().equals(defaultItem)) {
                    target.setSelectedIndex(i);
                    break;
                }
            }
        }
    }
}

Related

  1. getItems(JComboBox box)
  2. getItems(JComboBox comboBox)
  3. insertIntoCombo(JComboBox combo, Object item)
  4. items(JComboBox comboBox)
  5. loadCombo(JComboBox cmb, List data)
  6. loadStaticItems(JComboBox target, Class source, Class limit, String defaultItem)
  7. makeComboBox(Object[] items)
  8. replaceComboContents(JComboBox cb, String[] items)
  9. replaceComboItems(JComboBox combo, Vector items)