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:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "item1", "item2" };
    JComboBox cb = new JComboBox(items);

    Object obj = cb.getSelectedItem();

    cb.setSelectedItem("item2");
    obj = cb.getSelectedItem();//w w w  .java 2 s.com

    cb.setSelectedItem("item3");
    obj = cb.getSelectedItem();
}

From source file:AddingItemToComboBox.java

License:asdf

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox jComboBox1 = new JComboBox();
    jComboBox1.addItem("asdf");
    Object cmboitem = jComboBox1.getSelectedItem();
    System.out.println(cmboitem);

    frame.add(jComboBox1);//from  w  w w .  ja v  a2 s .  co  m

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

From source file:JComboBoxTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] selections = { "green", "red", "orange", "dark blue" };
    JComboBox comboBox = new JComboBox(selections);
    comboBox.setSelectedIndex(1);//from  www .ja  v a2 s . c  o m
    System.out.println(comboBox.getSelectedItem());
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a read-only combobox
    String[] items = { "item1", "item2" };
    JComboBox cb = new JComboBox(items);

    cb.setEditable(true);/*from w ww. ja  v a 2  s  .com*/
    cb.setSelectedItem("item3");
    Object obj = cb.getSelectedItem();

}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    JComboBox<Item> comboBox = new JComboBox<Item>(
            new Item[] { new Item("Major", "red"), new Item("Critical", "dark"), new Item("Minor", "green") });
    comboBox.addActionListener(e -> {
        JComboBox<Item> combo = (JComboBox<Item>) e.getSource();
        Item item = (Item) combo.getSelectedItem();
        System.out.println(item.getColor());
    });/*from  w w w  . ja  va  2 s  .c o  m*/
    JFrame frame = new JFrame();
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }//from www  .  j  a  v a2  s .  c o m
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

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

From source file:ActionTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            System.out.println("Modifiers: ");
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }//ww w .  j a  v a 2 s.  com
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

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

From source file:MainClass.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Change Look");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Object source = actionEvent.getSource();
            String lafClassName = null;
            if (source instanceof JComboBox) {
                JComboBox comboBox = (JComboBox) source;
                lafClassName = (String) comboBox.getSelectedItem();
            } else if (source instanceof JButton) {
                lafClassName = actionEvent.getActionCommand();
            }/*from w  ww . j av a2s .com*/
            if (lafClassName != null) {
                final String finalLafClassName = lafClassName;
                try {
                    UIManager.setLookAndFeel(finalLafClassName);
                    SwingUtilities.updateComponentTreeUI(frame);
                } catch (Exception exception) {
                    JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    };

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    DefaultComboBoxModel model = new DefaultComboBoxModel();
    JComboBox comboBox = new JComboBox(model);

    JPanel panel = new JPanel();

    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        model.addElement(looks[i].getClassName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        panel.add(button);
    }

    comboBox.addActionListener(actionListener);

    frame.add(comboBox, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.SOUTH);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

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 www .ja va  2s  .  c  o 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

public static void main(String[] args) {
    Object[] options = { "Option 1", "Option 2", "Option 3", "None of the above" };
    JComboBox optionControl = new JComboBox(options);
    optionControl.setSelectedIndex(3);/*from   w w  w .  j a v a2  s. c  o m*/
    JOptionPane.showMessageDialog(null, optionControl, "Option", JOptionPane.QUESTION_MESSAGE);
    System.out.println(optionControl.getSelectedItem());

    String graphSelection = (String) JOptionPane.showInputDialog(null, "Choose from the following options...",
            "Choose From DropDown", JOptionPane.QUESTION_MESSAGE, null, options, options[3]);
    System.out.println(graphSelection);

    JOptionPane.showMessageDialog(null, optionControl, "Option", JOptionPane.QUESTION_MESSAGE);
}