Java JComboBox Value replaceContents(JComboBox combob, Object[] values)

Here you can find the source of replaceContents(JComboBox combob, Object[] values)

Description

Replace the options in a combo box with a new set.

License

Apache License

Parameter

Parameter Description
combob GUI component to update
values list of values to put into component, or null to erase

Declaration

public static void replaceContents(JComboBox combob, Object[] values) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class Main {
    /**/*from w ww .  jav a2 s .  co m*/
     * Replace the options in a combo box with a new set.
     * nulls are skipped.  Selected value is preserved.
     *
     * @param combob GUI component to update
     * @param values list of values to put into component, or null to erase
     */
    public static void replaceContents(JComboBox combob, Object[] values) {
        Object cur = combob.getSelectedItem();
        DefaultComboBoxModel m = (DefaultComboBoxModel) combob.getModel();
        m.removeAllElements();
        if (values != null)
            for (Object v : values)
                if (v != null)
                    m.addElement(v);
        combob.setSelectedItem(cur);
        combob.setEnabled(true);
    }

    /**
     * Replace the options in a combo box with a new set.
     * nulls are skipped.  Selected value is preserved.
     *
     * @param combob GUI component to update
     * @param values list of values to put into component, or null to erase
     */
    public static void replaceContents(JComboBox combob, Vector<?> values) {
        Object cur = combob.getSelectedItem();
        DefaultComboBoxModel m = (DefaultComboBoxModel) combob.getModel();
        m.removeAllElements();
        if (values != null)
            for (Object v : values)
                if (v != null)
                    m.addElement(v);
        combob.setSelectedItem(cur);
        combob.setEnabled(true);
    }

    /**
     * Replace the options in a combo box with a new set.
     * nulls are skipped.  Selected value is preserved.
     *
     * @param combob GUI component to update
     * @param values list of values to put into component, or null to erase
     */
    public static void replaceContents(JComboBox combob, List<?> values) {
        Object cur = combob.getSelectedItem();
        DefaultComboBoxModel m = (DefaultComboBoxModel) combob.getModel();
        m.removeAllElements();
        if (values != null)
            for (Object v : values)
                if (v != null)
                    m.addElement(v);
        combob.setSelectedItem(cur);
        combob.setEnabled(true);
    }
}

Related

  1. getCmbValue(JComboBox cmb)
  2. getDouble(javax.swing.JComboBox input)
  3. getIfHasValue(Object val, JComboBox cb)
  4. getInt(JComboBox combo)
  5. IsComboBoxModified(JComboBox comboBox, Object originalValue)
  6. savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue)
  7. setComboBoxIndex(final JComboBox aComboBox, final int aIndex)
  8. value(JComboBox cb, Object[] val)