Java Utililty Methods JComboBox Item

List of utility methods to do JComboBox Item

Description

The list of methods to do JComboBox Item are organized into topic(s).

Method

Listitems(JComboBox comboBox)
items
ArrayList items = new ArrayList();
for (int i = 0; i < comboBox.getItemCount(); i++) {
    items.add(comboBox.getItemAt(i));
return items;
voidloadCombo(JComboBox cmb, List data)
load Combo
cmb.addItem(" ");
if (data != null) {
    Iterator it = data.iterator();
    while (it.hasNext()) {
        cmb.addItem(it.next());
voidloadInstances(JComboBox target, Class source, Class limit, Object defaultItem)
load Instances
Class<?>[] clss = source.getClasses();
for (Class<?> cls : clss) {
    try {
        if (!limit.isAssignableFrom(cls)) {
            continue;
        if (cls.isInterface()) {
            continue;
...
voidloadStaticItems(JComboBox target, Class source, Class limit, String defaultItem)
load Static Items
Field[] fields = source.getFields();
for (Field f : fields) {
    try {
        if (!java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
            continue;
        Object v = f.get(null);
        if (v == null) {
...
JComboBoxmakeComboBox(Object[] items)
Creates a combo box that remains one line in height.
return new JComboBox(items) {
    public Dimension getMaximumSize() {
        Dimension pref = getPreferredSize();
        Dimension max = super.getMaximumSize();
        return new Dimension(max.width, pref.height);
};
voidreplaceComboContents(JComboBox cb, String[] items)
replace Combo Contents
ActionListener[] listeners = cb.getActionListeners();
for (int i = 0; i < listeners.length; i++)
    cb.removeActionListener(listeners[i]);
if (cb.getItemCount() > 0)
    cb.removeAllItems();
for (int i = 0; i < items.length; i++) {
    cb.addItem(items[i]);
for (int i = 0; i < listeners.length; i++)
    cb.addActionListener(listeners[i]);
voidreplaceComboItems(JComboBox combo, Vector items)
Replace all items in a JComboBox (What am I missing in the published API?)
combo.removeAllItems();
Iterator scit = items.iterator();
while (scit.hasNext()) {
    combo.addItem(scit.next());
return;
voidselJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name)
Sets the selection of the JComboBox to the Object 'name' from the list in namVec.
int idx = namVec.indexOf(name);
combo.setSelectedIndex(idx);
combo.updateUI();
voidselJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name)
Sets the selection of the JComboBox to the Object 'name' from the list in namVec.
int idx = namVec.indexOf(name);
combo.setSelectedIndex(idx);
combo.updateUI();
voidsetListData(JComboBox box, Object[] items)
Procedure to set the list of items in a ComboBox
box.removeAllItems();
if (items != null) {
    for (int i = 0; i < items.length; i++) {
        box.addItem(items[i]);