JComboBox contains Value - Java Swing

Java examples for Swing:JComboBox

Description

JComboBox contains Value

Demo Code


//package com.java2s;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

public class Main {
    /**/*  w  w w  . j av  a 2  s  .c o m*/
     * @param comboBox
     * @param value
     * @return if the comboBox contains the specified value
     */
    public static boolean containsValue(JComboBox comboBox, String value) {
        ComboBoxModel model = comboBox.getModel();
        int size = model.getSize();
        for (int i = 0; i < size; i++) {
            Object element = model.getElementAt(i);
            if (element.equals(value)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials