Example usage for javax.swing JComboBox setModel

List of usage examples for javax.swing JComboBox setModel

Introduction

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

Prototype

@BeanProperty(description = "Model that the combo box uses to get data to display.")
public void setModel(ComboBoxModel<E> aModel) 

Source Link

Document

Sets the data model that the JComboBox uses to obtain the list of items.

Usage

From source file:Main.java

public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);

    JFrame frame = new JFrame("Shared Data");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    JComboBox comboBox1 = new JComboBox();
    comboBox1.setModel(model);
    comboBox1.setMaximumRowCount(5);//from w w  w.ja  va  2 s.c  o  m
    comboBox1.setEditable(true);

    JComboBox comboBox2 = new JComboBox(model);
    comboBox2.setMaximumRowCount(5);
    comboBox2.setEditable(true);
    panel.add(comboBox1);
    panel.add(comboBox2);
    frame.add(panel, BorderLayout.NORTH);

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

From source file:Main.java

public static void main(String[] args) {
    JComboBox<Object> combo = new JComboBox<>();
    URL url = new Main().getClass().getResource("animated.gif");
    combo.setModel(new DefaultComboBoxModel(new Object[] { makeImageIcon(url, combo, 0) }));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(combo);//from  ww  w  . ja va 2 s .c om
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:PopupTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup Menu Listener");
    Container contentPane = frame.getContentPane();

    final String flavors[] = { "Item 1", "Item 2", "Item 3" };

    PopupMenuListener listener = new PopupMenuListener() {
        boolean initialized = false;

        public void popupMenuCanceled(PopupMenuEvent e) {
        }//from  ww  w. ja v a2 s.c  o  m

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (!initialized) {
                JComboBox combo = (JComboBox) e.getSource();
                ComboBoxModel model = new DefaultComboBoxModel(flavors);
                combo.setModel(model);
                initialized = true;
            }
        }
    };

    JComboBox jc = new JComboBox();
    jc.addPopupMenuListener(listener);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    contentPane.add(jc, BorderLayout.NORTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup Menu Listener");
    Container contentPane = frame.getContentPane();

    final String flavors[] = { "Item 1", "Item 2", "Item 3" };

    PopupMenuListener listener = new PopupMenuListener() {
        boolean initialized = false;

        public void popupMenuCanceled(PopupMenuEvent e) {
        }/*  w ww.j  ava2 s . com*/

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (!initialized) {
                JComboBox combo = (JComboBox) e.getSource();
                ComboBoxModel model = new DefaultComboBoxModel(flavors);
                combo.setModel(model);
                initialized = true;
            }
        }
    };

    JComboBox jc = new JComboBox();
    jc.addPopupMenuListener(listener);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    contentPane.add(jc, BorderLayout.NORTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new GridLayout(0, 1, 5, 5));

    String[] speciesName = { "1", "2", "3" };
    String[][] breedName = { { "A", "P", "A" }, { "B", "P", "S" }, { "DDo", "A", "P" } };
    JComboBox<String> petSpecies = new JComboBox<>(speciesName);
    JComboBox<String> petBreed = new JComboBox<>();
    petSpecies.addItemListener(e -> {
        int ii = petSpecies.getSelectedIndex();
        ComboBoxModel cbm = new DefaultComboBoxModel(breedName[ii]);
        petBreed.setModel(cbm);
        petBreed.requestFocusInWindow();
    });//from w ww  . j av a  2s .c  o m
    gui.add(petSpecies);
    gui.add(petBreed);

    JOptionPane.showMessageDialog(null, gui);
}

From source file:OAT.ui.util.UiUtil.java

public static void fillCombo(JComboBox comboBox, Collection items) {
    comboBox.setModel(new DefaultComboBoxModel(items.toArray()));
}

From source file:eu.dety.burp.joseph.utilities.Converter.java

/**
 * Get RSA PublicKey by PublicKey HashMap input. Create a dialog popup with a combobox to choose the correct JWK to use.
 * /* w  w w . j  a  v a  2  s .  c  om*/
 * @param publicKeys
 *            HashMap containing a PublicKey and related describing string
 * @throws AttackPreparationFailedException
 * @return Selected {@link PublicKey}
 */
@SuppressWarnings("unchecked")
public static PublicKey getRsaPublicKeyByJwkSelectionPanel(HashMap<String, PublicKey> publicKeys)
        throws AttackPreparationFailedException {
    // TODO: Move to other class?
    JPanel selectionPanel = new JPanel();
    selectionPanel.setLayout(new java.awt.GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;

    constraints.gridy = 0;
    selectionPanel.add(new JLabel("Multiple JWKs found. Please choose one:"), constraints);

    JComboBox jwkSetKeySelection = new JComboBox<>();
    DefaultComboBoxModel<String> jwkSetKeySelectionModel = new DefaultComboBoxModel<>();

    for (Map.Entry<String, PublicKey> publicKey : publicKeys.entrySet()) {
        jwkSetKeySelectionModel.addElement(publicKey.getKey());
    }

    jwkSetKeySelection.setModel(jwkSetKeySelectionModel);

    constraints.gridy = 1;
    selectionPanel.add(jwkSetKeySelection, constraints);

    int resultButton = JOptionPane.showConfirmDialog(null, selectionPanel, "Select JWK",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

    if (resultButton == JOptionPane.CANCEL_OPTION) {
        throw new AttackPreparationFailedException("No JWK from JWK Set selected!");
    }

    loggerInstance.log(Converter.class, "Key selected: " + jwkSetKeySelection.getSelectedIndex(),
            Logger.LogLevel.DEBUG);
    return publicKeys.get(jwkSetKeySelection.getSelectedItem());
}

From source file:eu.europa.ec.markt.dss.applet.util.ComponentFactory.java

/**
 * //from  w  w  w .  j  a  va  2s.com
 * @param model
 * @param actionListener
 * @return
 */
public static JComboBox combo(final ComboBoxModel model, final ActionListener actionListener) {
    final JComboBox combo = new JComboBox();
    combo.setModel(model);
    combo.addActionListener(actionListener);
    return combo;
}

From source file:org.jdal.swing.form.FormUtils.java

/**
 * Add a link on primary and dependent JComboBoxes by property name. 
 * When selection changes on primary use propertyName to get a Collection and fill dependent JComboBox with it
 * @param primary JComboBox when selection changes
 * @param dependent JComboBox that are filled with collection   
 * @param propertyName the property name for get the collection from primary selected item
 * @param addNull if true, add a null as first combobox item
 *//*w  ww  .  j  ava  2 s  . c  o m*/
@SuppressWarnings("rawtypes")
public static void link(final JComboBox primary, final JComboBox dependent, final String propertyName,
        final boolean addNull) {

    primary.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Object selected = primary.getSelectedItem();
            if (selected != null) {
                BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(selected);
                if (wrapper.isWritableProperty(propertyName)) {
                    Collection collection = (Collection) wrapper.getPropertyValue(propertyName);
                    Vector<Object> vector = new Vector<Object>(collection);
                    if (addNull)
                        vector.add(0, null);
                    DefaultComboBoxModel model = new DefaultComboBoxModel(vector);
                    dependent.setModel(model);
                } else {
                    log.error("Can't write on propety '" + propertyName + "' of class: '" + selected.getClass()
                            + "'");
                }
            }
        }
    });
}

From source file:EHRAppointment.ChartPanelDraw.java

private JComboBox createTrace() { //Create mouse trace
    final JComboBox trace = new JComboBox();
    final String[] traceCmds = { "Enable Trace", "Disable Trace" };
    trace.setModel(new DefaultComboBoxModel(traceCmds));
    trace.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (traceCmds[0].equals(trace.getSelectedItem())) {
                chartPanel.setHorizontalAxisTrace(true);
                chartPanel.setVerticalAxisTrace(true);
                chartPanel.repaint();/*  w  ww . j  ava2s.  c  o  m*/
            } else {
                chartPanel.setHorizontalAxisTrace(false);
                chartPanel.setVerticalAxisTrace(false);
                chartPanel.repaint();
            }
        }
    });
    return trace;
}