Example usage for javax.swing DefaultComboBoxModel removeAllElements

List of usage examples for javax.swing DefaultComboBoxModel removeAllElements

Introduction

In this page you can find the example usage for javax.swing DefaultComboBoxModel removeAllElements.

Prototype

public void removeAllElements() 

Source Link

Document

Empties the list.

Usage

From source file:Main.java

public static void main(final String args[]) {
    final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    model.addElement("A");
    model.addElement("C");
    model.addElement("D");
    model.addElement("A");

    model.removeAllElements();

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox<String> comboBox1 = new JComboBox<String>(model);
    comboBox1.setMaximumRowCount(5);/*from  ww  w . j a v  a  2 s .c o  m*/
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);

    JList<String> jlist = new JList<String>(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("a");
            model.insertElementAt("Z", 0);
        }
    };
    button.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String[] mainData = { "-Select-", "Sel 1", "Sel 2", "Sel 3" };
    String[] subData1 = { "Sub Sel 11", "Sub Sel 12", "Sub Sel 13" };
    String[] subData2 = { "Sub Sel 21", "Sub Sel 22", "Sub Sel 23" };
    String[] subData3 = { "Sub Sel 31", "Sub Sel 32", "Sub Sel 33" };
    DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
    JComboBox box1 = new JComboBox(mainData), box2 = new JComboBox();
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.add(box1);//  w w w . j  av a2  s  .c  o m
    frame.add(box2);
    box2.setVisible(false);
    box2.setModel(boxModel);
    frame.setBounds(200, 200, 500, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    box1.addItemListener(e -> {
        box2.setVisible(true);
        boxModel.removeAllElements();
        if (box1.getSelectedIndex() == 0) {
            box2.setVisible(false);
        } else if (box1.getSelectedIndex() == 1) {
            for (String s : subData1) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 2) {
            for (String s : subData2) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 3) {
            for (String s : subData3) {
                boxModel.addElement(s);
            }
        }
    });
}

From source file:Main.java

public static void initComboBoxModel(ComboBoxModel model, Object[] lista) {
    DefaultComboBoxModel defCombo = (DefaultComboBoxModel) model;
    defCombo.removeAllElements();
    for (Object o : lista) {
        defCombo.addElement(o);/*  w  w w  . j a  v a2  s.c  om*/
    }
}

From source file:Main.java

public static void initComboBoxModel(ComboBoxModel model, List lista) {
    DefaultComboBoxModel defCombo = (DefaultComboBoxModel) model;
    defCombo.removeAllElements();
    for (Object o : lista) {
        defCombo.addElement(o);/* w  w  w  .j a  va  2  s.  c o  m*/
    }
}

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  v a2  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.  ja v  a 2  s . co 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: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 .java 2 s .c  om*/
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

/**
 * Setzt die Daten des Iterators in das ComboBoxModel.
 * /*  w  w w  .  jav a 2 s . c  om*/
 * @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: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();

    model.addElement(SELECT_A_SONG);/*  www .j  a  va2  s  .  c  o m*/
    for (Song s : songs)
        model.addElement(s);

    AutoCompleteDecorator.decorate(cb, SONG_COMBO_CONVERTER);
}

From source file:Main.java

private void setContents(Node node) {
    if (node == null) {
        return;/*  w  w w .  java 2  s.c  o  m*/
    }
    ValueName.setText(node.toString());
    DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();
    model.removeAllElements();
    if (node.getValues().length > 0) {
        panel.add(comboBox);
        for (String s : node.getValues()) {
            model.addElement(s);
        }
        comboBox.setSelectedIndex(node.getMyIndex());
    } else {
        panel.remove(comboBox);
    }
}