Example usage for javax.swing JComboBox getModel

List of usage examples for javax.swing JComboBox getModel

Introduction

In this page you can find the example usage for javax.swing JComboBox getModel.

Prototype

public ComboBoxModel<E> getModel() 

Source Link

Document

Returns the data model currently used by the JComboBox.

Usage

From source file:Main.java

public static void main(String[] args) {
    Object[] items = new Object[] { "Dog", "Cat", "Other" };
    DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);
    JComboBox comboBox = new JComboBox(dcbm);
    comboBox.setPreferredSize(new Dimension(200, 20));
    comboBox.addItemListener(e -> {//from w w w . j a  v a  2s .co m
        Object selectedItem = comboBox.getSelectedItem();
        boolean editable = selectedItem instanceof String && ((String) selectedItem).equals("Other");
        comboBox.setEditable(editable);
    });
    comboBox.getEditor().addActionListener(e -> {
        Object newItem = comboBox.getEditor().getItem();
        DefaultComboBoxModel d = (DefaultComboBoxModel) comboBox.getModel();
        d.addElement(newItem);
        d.setSelectedItem(newItem);

    });

    JPanel content = new JPanel(new FlowLayout());
    content.add(new JLabel("Test:"));
    content.add(comboBox);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(content);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * @param comboBox/*from  w  ww  . j  a  v a2 s.c o m*/
 * @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;
}

From source file:Main.java

/**
 * Setzt die Daten des Iterators in das ComboBoxModel.
 * /*from w ww  . java 2  s . co  m*/
 * @param comboBox {@link JComboBox}
 * @param iterator {@link Iterator}
 */
@SuppressWarnings("unchecked")
public static void fillComboBox(final JComboBox<?> comboBox, final Iterator<?> iterator) {
    DefaultComboBoxModel<Object> comboBoxModel = (DefaultComboBoxModel<Object>) comboBox.getModel();
    comboBoxModel.removeAllElements();

    while (iterator.hasNext()) {
        comboBoxModel.addElement(iterator.next());
    }
}

From source file:Main.java

/**
 * 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
 *//*ww  w.j  a  v  a 2  s  . co m*/
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);
}

From source file:Main.java

/**
 * 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
 *//*from   w w w .j a va  2  s  .com*/
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);
}

From source file:Main.java

/**
 * 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
 *//*from ww  w. j a  v a 2s  . c  o m*/
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);
}

From source file:jshm.gui.GuiUtil.java

public static void createSongCombo(JComboBox cb, List<? extends Song> songs) {
    cb.setRenderer(SONG_COMBO_RENDERER);
    DefaultComboBoxModel model = (DefaultComboBoxModel) cb.getModel();
    model.removeAllElements();/*from   w  ww .ja  v  a 2 s .com*/

    model.addElement(SELECT_A_SONG);
    for (Song s : songs)
        model.addElement(s);

    AutoCompleteDecorator.decorate(cb, SONG_COMBO_CONVERTER);
}

From source file:Main.java

public Main() {
    JComboBox jc = new JComboBox();
    jc.addItem("France");
    jc.addItem("Germany");
    jc.addItem("Italy");
    jc.addItem("Japan");
    jc.addItemListener(this);
    add(jc);// w  w  w.j av  a 2  s  .  c  o m
    ComboBoxModel model = jc.getModel();
}

From source file:Main.java

public void keyPressed(KeyEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    int curIx = cb.getSelectedIndex();

    char ch = evt.getKeyChar();

    JComboBox.KeySelectionManager ksm = cb.getKeySelectionManager();
    if (ksm != null) {
        int ix = ksm.selectionForKey(ch, cb.getModel());
        boolean noMatch = ix < 0;
        boolean uniqueItem = ix == curIx;

        if (noMatch || !uniqueItem) {
            cb.showPopup();// w ww . j a  v  a2s.c  om
        }
    }
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();
    System.out.println("down action");
    ComboBoxUI ui = jcb.getUI();//from w w w.j  ava  2 s.  c om

    if (ui.isPopupVisible(jcb)) {
        int i = jcb.getSelectedIndex();
        if (i < jcb.getModel().getSize() - 1) {
            jcb.setSelectedIndex(i + 1);
            jcb.repaint();
        }
    } else {
        int nItems = jcb.getItemCount();

        ComboBoxEditor cbe = jcb.getEditor();

        String st; // Search text

        st = ((String) cbe.getItem()).toUpperCase();

        for (int i = 0; i < nItems; i++) {
            String item = ((String) jcb.getItemAt(i)).toUpperCase();

            if (item.startsWith(st)) {
                jcb.setSelectedIndex(i);
                break;
            }
        }

        ui.setPopupVisible(jcb, true);
    }
}