Description

load Static Items

License

Open Source License

Declaration

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

Method Source Code


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

import java.lang.reflect.Field;

import javax.swing.JComboBox;

public class Main {
    @SuppressWarnings("unchecked")
    public static <A, B> void loadStaticItems(JComboBox<B> target, Class<A> source, Class<?> limit,
            String defaultItem) {
        Field[] fields = source.getFields();
        for (Field f : fields) {
            try {
                if (!java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
                    continue;
                }//from   w ww.  j ava 2  s .c o m
                Object v = f.get(null);
                if (v == null) {
                    continue;
                }
                if (limit.isInstance(v)) {
                    target.addItem((B) v);
                }
            } catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
                System.err.println("Error intializing GUI:" + f.getName());
                e.printStackTrace();
            }
        }

        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 comboBox)
  2. insertIntoCombo(JComboBox combo, Object item)
  3. items(JComboBox comboBox)
  4. loadCombo(JComboBox cmb, List data)
  5. loadInstances(JComboBox target, Class source, Class limit, Object defaultItem)
  6. makeComboBox(Object[] items)
  7. replaceComboContents(JComboBox cb, String[] items)
  8. replaceComboItems(JComboBox combo, Vector items)
  9. selJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name)