Example usage for javax.swing JComboBox getSelectedItem

List of usage examples for javax.swing JComboBox getSelectedItem

Introduction

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

Prototype

public Object getSelectedItem() 

Source Link

Document

Returns the current selected item.

Usage

From source file:ComboBoxDemo.java

/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    String petName = (String) cb.getSelectedItem();
    updateLabel(petName);/*from  w  w  w  .  j a  v a  2  s.  c o m*/
}

From source file:ComboBoxDemo2.java

public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    String newSelection = (String) cb.getSelectedItem();
    currentPattern = newSelection;// www.  j  av a  2  s. c o  m
    reformat();
}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(320, 240));
    setBackground(hue.getColor());//from w ww  .j  a v a 2 s.c o  m
    JComboBox colorBox = new JComboBox();
    for (Hue h : Hue.values()) {
        colorBox.addItem(h);
    }
    colorBox.addActionListener(e -> {
        Hue h = (Hue) colorBox.getSelectedItem();
        Main.this.setBackground(h.getColor());
    });
    this.add(colorBox);
}

From source file:app.Plot.java

public void showElements(List<WordStatistic> was, JComboBox cb) {

    JFreeChart chart = createPlot(was, cb.getSelectedItem());
    plotDesign(chart);/*from  w  w w  .  j a  va  2  s . c  om*/
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setDomainZoomable(false);
    PlotWindow plot = new PlotWindow();
    plot.setVisible(true);
    JPanel panetJTitle = new JPanel();
    JPanel panelJPanel = new JPanel();
    JPanel panelJComboBox = new JPanel();
    panelJPanel.removeAll();
    panelJComboBox.removeAll();
    JLabel title = new JLabel();
    title.setText(
            " ?? ? ? ??  '"
                    + cb.getSelectedItem().toString() + "'   ?");
    chartPanel.setPreferredSize(new java.awt.Dimension(600, 400));
    JLabel label = new JLabel();
    label.setText(" ?? ");
    panetJTitle.add(title);
    panelJPanel.add(chartPanel);
    panelJComboBox.add(label);
    panelJComboBox.add(cb);
    JPanel panelWindow = new JPanel();
    panelWindow.add(panetJTitle);
    panelWindow.add(panelJPanel);
    panelWindow.add(panelJComboBox);
    plot.setContentPane(panelWindow);

}

From source file:MainClass.java

public MainClass() {
    Container cp = new Box(BoxLayout.X_AXIS);
    setContentPane(cp);//from ww w  .  j  a  va 2s . com
    JPanel firstPanel = new JPanel();
    propertyComboBox = new JComboBox();
    propertyComboBox.addItem("text");
    propertyComboBox.addItem("font");
    propertyComboBox.addItem("background");
    propertyComboBox.addItem("foreground");
    firstPanel.add(propertyComboBox);
    cp.add(firstPanel);
    cp.add(Box.createGlue());

    tf = new JTextField("Hello");
    tf.setForeground(Color.RED);
    tf.setDragEnabled(true);
    cp.add(tf);

    cp.add(Box.createGlue());

    l = new JLabel("Hello");
    l.setBackground(Color.YELLOW);
    cp.add(l);

    cp.add(Box.createGlue());

    JSlider stryder = new JSlider(SwingConstants.VERTICAL);
    stryder.setMinimum(10);
    stryder.setValue(14);
    stryder.setMaximum(72);
    stryder.setMajorTickSpacing(10);
    stryder.setPaintTicks(true);

    cp.add(stryder);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);

    setMyTransferHandlers((String) propertyComboBox.getSelectedItem());

    MouseListener myDragListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    };
    l.addMouseListener(myDragListener);

    propertyComboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ce) {
            JComboBox bx = (JComboBox) ce.getSource();
            String prop = (String) bx.getSelectedItem();
            setMyTransferHandlers(prop);
        }
    });

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JTextField jtf = (JTextField) evt.getSource();
            String fontName = jtf.getText();
            Font font = new Font(fontName, Font.BOLD, 18);
            tf.setFont(font);
        }
    });

    stryder.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JSlider sl = (JSlider) evt.getSource();
            Font oldf = tf.getFont();
            Font newf = oldf.deriveFont((float) sl.getValue());
            tf.setFont(newf);
        }
    });
}

From source file:cl.almejo.vsim.gui.actions.preferences.ColorPreferences.java

private JComboBox<String> createSchemeCombobox() {
    JComboBox<String> _comboBox = new JComboBox<String>(ColorScheme.getNames());
    _comboBox.addActionListener(new ActionListener() {
        @Override//w  ww.  ja v a  2 s  . c  o  m
        public void actionPerformed(ActionEvent e) {
            JComboBox combobox = (JComboBox) e.getSource();
            ColorScheme.setCurrent((String) combobox.getSelectedItem());
        }
    });
    _comboBox.setSelectedItem("default");
    return _comboBox;
}

From source file:ComboBoxWithItemChangedListener.java

public ComboPanel() {
    final JComboBox combo = new JComboBox(treasure);
    combo.setMaximumRowCount(5);/*from  www . j a v a2 s . c  o  m*/
    combo.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Combo: " + combo.getSelectedItem());
        }
    });
    combo.setSelectedIndex(4);
    add(combo);
}

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   www.  java  2 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:Transfer.java

public Transfer() {

    // Establish the GUI
    Container cp = new Box(BoxLayout.X_AXIS);
    setContentPane(cp);//  ww w .  j  a v a2  s  . com
    JPanel firstPanel = new JPanel();
    propertyComboBox = new JComboBox();
    propertyComboBox.addItem("text");
    propertyComboBox.addItem("font");
    propertyComboBox.addItem("background");
    propertyComboBox.addItem("foreground");
    firstPanel.add(propertyComboBox);
    cp.add(firstPanel);
    cp.add(Box.createGlue());

    tf = new JTextField("Hello");
    tf.setForeground(Color.RED);
    tf.setDragEnabled(true);
    cp.add(tf);

    cp.add(Box.createGlue());

    l = new JLabel("Hello");
    l.setBackground(Color.YELLOW);
    cp.add(l);

    cp.add(Box.createGlue());

    JSlider stryder = new JSlider(SwingConstants.VERTICAL);
    stryder.setMinimum(10);
    stryder.setValue(14);
    stryder.setMaximum(72);
    stryder.setMajorTickSpacing(10);
    stryder.setPaintTicks(true);

    cp.add(stryder);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);

    // Add Listeners and Converters
    setMyTransferHandlers((String) propertyComboBox.getSelectedItem());

    // Mousing in the Label starts a Drag.
    MouseListener myDragListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    };
    l.addMouseListener(myDragListener);

    // Selecting in the ComboBox makes that the property that is xfered.
    propertyComboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ce) {
            JComboBox bx = (JComboBox) ce.getSource();
            String prop = (String) bx.getSelectedItem();
            setMyTransferHandlers(prop);
        }
    });

    // Typing a word and pressing enter in the TextField tries
    // to set that as the font name.
    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JTextField jtf = (JTextField) evt.getSource();
            String fontName = jtf.getText();
            Font font = new Font(fontName, Font.BOLD, 18);
            tf.setFont(font);
        }
    });

    // Setting the Slider sets that font into the textfield.
    stryder.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JSlider sl = (JSlider) evt.getSource();
            Font oldf = tf.getFont();
            Font newf = oldf.deriveFont((float) sl.getValue());
            tf.setFont(newf);
        }
    });

}

From source file:Main.java

public Main() {
    JComboBox<String> cb = new JComboBox<>(new String[] { "a", "m", "b", "c", "v" });
    cb.setSelectedIndex(-1);//from  ww  w . ja  v  a 2s .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);
}