Java Utililty Methods JComboBox

List of utility methods to do JComboBox

Description

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

Method

booleancomboContainsElement(JComboBox combo, String element)
Return true if the combo contains the element, return false otherwise
for (int i = 0; i < combo.getItemCount(); i++) {
    if (combo.getItemAt(i).equals(element)) {
        return true;
return false;
booleancontains(JComboBox jcb, Object o, boolean caseSensitive)
contains
if (o == null)
    return false;
if (caseSensitive || !(o instanceof String)) {
    for (int i = 0; i < jcb.getItemCount(); i++)
        if (jcb.getItemAt(i).equals(o))
            return true;
} else {
    String s = ((String) o).toLowerCase();
...
JComponentcreateLabelCombo(String label, JComboBox comboBox)
Create a GUI component JLabel + JComboBox with a left and right margin (5px)
JPanel labelCombo = new JPanel();
labelCombo.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JLabel caption = new JLabel(label);
caption.setBorder(new EmptyBorder(0, 5, 0, 5));
labelCombo.add(caption);
labelCombo.add(comboBox);
return labelCombo;
ActionListener[]disableListenersForComboBox(JComboBox comboBox)
disable Listeners For Combo Box
ActionListener[] listeners = comboBox.getActionListeners();
for (ActionListener listener : listeners) {
    comboBox.removeActionListener(listener);
return listeners;
voidensureCustomBackgroundStored(JComboBox comp)
ensure Custom Background Stored
if (getStoredBackground(comp) != null)
    return;
Color background = comp.getBackground();
if ((background == null) || (background instanceof UIResource) || (background == WARNING_BACKGROUND)
        || (background == ERROR_BACKGROUND))
    return;
comp.putClientProperty(STORED_BACKGROUND_KEY, background);
booleanexist(JComboBox x)
Return true if the list is not empty, false otherwise
if (x.getSelectedIndex() == -1) {
    JOptionPane.showMessageDialog(x, "Empty list.", "Message", JOptionPane.ERROR_MESSAGE);
    return false;
return true;
voidfixComboOrientation(JComboBox combo)
Fixes the orientation of the renderer of a combo box.
ListCellRenderer r = combo.getRenderer();
if (r instanceof Component) {
    ComponentOrientation o = ComponentOrientation.getOrientation(Locale.getDefault());
    ((Component) r).setComponentOrientation(o);
JComboBoxgenerateComboBoxFor(Object... objs)
generate Combo Box For
Container c = search(objs, Container.class);
ComboBoxModel model = null;
Vector items = null;
Object[] itms = null;
Border border = search(objs, Border.class);
Color color = search(objs, Color.class);
ComboBoxUI ui = search(objs, ComboBoxUI.class);
JComboBox combo = (model = search(objs, ComboBoxModel.class)) == null
...
JComboBoxgetAgeComboBox()
get Age Combo Box
JComboBox<Integer> jcb = new JComboBox<Integer>();
for (int id : age.keySet()) {
    jcb.addItem(id);
return jcb;
ColorgetComboBoxDisabledBackground()
get Combo Box Disabled Background
return UIManager.getColor("ComboBox.disabledBackground");