Example usage for javax.swing JComboBox getSelectedIndex

List of usage examples for javax.swing JComboBox getSelectedIndex

Introduction

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

Prototype

@Transient
public int getSelectedIndex() 

Source Link

Document

Returns the first item in the list that matches the given item.

Usage

From source file:Main.java

public static Object getSelectedValue(JComboBox cb, Object[] val) {
    int selIndex = cb.getSelectedIndex();
    if (selIndex == -1)
        return null;
    return val[selIndex];
}

From source file:Main.java

static ImageIcon makeImageIcon(URL url, JComboBox combo, int row) {
    ImageIcon icon = new ImageIcon(url);
    icon.setImageObserver(new ImageObserver() {
        public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
            if (combo.isShowing() && (infoflags & (FRAMEBITS | ALLBITS)) != 0) {
                if (combo.getSelectedIndex() == row) {
                    combo.repaint();//  w  ww. j a  va  2s  .c  o m
                }
                BasicComboPopup p = (BasicComboPopup) combo.getAccessibleContext().getAccessibleChild(0);
                JList list = p.getList();
                if (list.isShowing()) {
                    list.repaint(list.getCellBounds(row, row));
                }
            }
            return (infoflags & (ALLBITS | ABORT)) == 0;
        };
    });
    return icon;
}

From source file:Main.java

public static int select(String[] selList, String msg) {
    JComboBox<String> box = new JComboBox<>(selList);
    Object msgs[] = new Object[2];
    msgs[0] = msg;//from   w ww  .ja v  a  2s . co  m
    msgs[1] = box;
    int result = JOptionPane.showOptionDialog(null, msgs, msg, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (result == JOptionPane.CANCEL_OPTION)
        return -1;
    return box.getSelectedIndex();
}

From source file:com.willwinder.ugs.platform.probe.ProbeTopComponent.java

private static WorkCoordinateSystem get(JComboBox<WorkCoordinateSystem> wcsCombo) {
    return wcsCombo.getItemAt(wcsCombo.getSelectedIndex());
}

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

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) {
        // Determine if another item has the same prefix
        int ix = ksm.selectionForKey(ch, cb.getModel());
        boolean noMatch = ix < 0;
        boolean uniqueItem = ix == curIx;

        // Display menu if no matching items or the if the selection is not unique
        if (noMatch || !uniqueItem) {
            cb.showPopup();//from w  w w.  j  a v  a  2s  .c  om
        }
    }
}

From source file:ComboBox.java

public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        JComboBox combo = (JComboBox) e.getSource();
        int index = combo.getSelectedIndex();
        display.setIcon(new ImageIcon(ClassLoader.getSystemResource(images[index])));
    }//from ww w  .  j av a  2  s .c  o  m
}

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.
 * //from   w w  w .j av  a2 s .  c  o  m
 * @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:AlphaCompositeDemo.java

public AlphaCompositeDemo() {
    super();//from ww w.  jav  a2s  .co m
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    rulesBox = new JComboBox(rulesLabels);
    rulesBox.setSelectedIndex(0);
    rulesBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    rulesBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            canvas.compositeRule = rules[cb.getSelectedIndex()];
            canvas.repaint();
        }
    });

    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(25);
    slider.setMinorTickSpacing(25);
    slider.setPaintLabels(true);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            canvas.alphaValue = (float) slider.getValue() / 100;
            canvas.repaint();
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 3));
    panel.add(rulesBox);
    panel.add(new JLabel("Alpha Adjustment x E-2: ", JLabel.RIGHT));
    panel.add(slider);
    container.add(panel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(500, 300);
    setVisible(true);
}

From source file:Main.java

public Main() {
    JComboBox<String> cb = new JComboBox<>(new String[] { "a", "m", "b", "c", "v" });
    cb.setSelectedIndex(-1);/* w w  w.  jav  a 2 s . co m*/

    JButton button = new JButton("Print Selection");
    button.addActionListener(e -> {
        if (cb.getSelectedIndex() != -1)
            System.out.println(cb.getSelectedItem());
        else
            System.out.println("Not selected");
    });

    add(cb);
    add(button);
}