set JComboBox Selection - Java Swing

Java examples for Swing:JComboBox

Description

set JComboBox Selection

Demo Code

// LICENSE:      This file is distributed under the BSD license.
//package com.java2s;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;

public class Main {
    public static void setComboSelection(JComboBox cb, String sel) {
        ActionListener[] listeners = cb.getActionListeners();
        for (int i = 0; i < listeners.length; i++)
            cb.removeActionListener(listeners[i]);
        cb.setSelectedItem(sel);/*  w  w  w  . j a va 2  s  .  c o  m*/
        for (int i = 0; i < listeners.length; i++)
            cb.addActionListener(listeners[i]);
    }
}

Related Tutorials