Here you can find the source of loadInstances(JComboBox target, Class> source, Class> limit, Object defaultItem)
@SuppressWarnings("unchecked") public static <B> void loadInstances(JComboBox<B> target, Class<?> source, Class<?> limit, Object defaultItem)
//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; } } } } }