Example usage for javax.swing JComboBox JComboBox

List of usage examples for javax.swing JComboBox JComboBox

Introduction

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

Prototype

public JComboBox(Vector<E> items) 

Source Link

Document

Creates a JComboBox that contains the elements in the specified Vector.

Usage

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);/*  w  ww  .  j av a 2  s .com*/
    System.out.println(comboBox.getSelectedItem());
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    String[] values = new String[] { "One", "Two", "Three" };
    JComboBox<String> comboBox = new JComboBox<>(values);
    panel.add(comboBox, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea(2, 2);
    panel.add(textArea, BorderLayout.CENTER);
    JButton button = new JButton("Action");
    button.addActionListener(new ActionListener() {
        @Override// ww w .j  av a  2  s.c om
        public void actionPerformed(ActionEvent e) {
            textArea.setText((String) comboBox.getSelectedItem());
            comboBox.setSelectedIndex(0);
        }
    });
    panel.add(button, BorderLayout.SOUTH);
    JFrame frame = new JFrame();
    frame.add(panel);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
        }/*from ww  w . ja v a  2s. c o  m*/
    };
    comboBox.addActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
        }/*  w w  w  . j ava  2  s.c  o  m*/
    };
    comboBox.addActionListener(actionListener);
    comboBox.removeActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            System.out.println("Item: " + itemEvent.getItem());
            System.out.println(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            System.out.println(", Selected: " + selectedString(is));
        }//w w  w .j a v  a 2 s.  co  m
    };
    comboBox.addItemListener(itemListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            System.out.println(", Selected: " + selectedString(is));
            System.out.println("paramString:" + actionEvent.paramString());
        }/*from   w  w w .  j  a v a2 s . c  om*/
    };
    comboBox.addActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fonts = ge.getAvailableFontFamilyNames();
    JComboBox<String> fontChooser = new JComboBox<>(fonts);
    fontChooser.setRenderer(new FontCellRenderer());
    JOptionPane.showMessageDialog(null, fontChooser);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    String[] list = { "Hello 1", "Hello 2", "Hello 3", "Hello 4" };
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(list);
    JComboBox<String> comboBox = new JComboBox<>(model);
    frame.add(comboBox, BorderLayout.NORTH);

    final JTextField textField = new JTextField(30);
    frame.add(textField, BorderLayout.SOUTH);
    frame.add(new JLabel("Type something, then press enter", JLabel.CENTER));

    textField.addActionListener(ae -> {
        String text = textField.getText();
        model.addElement(text);//from  ww w  .j  a  v  a  2 s  .c o m
        comboBox.setSelectedItem(text);
        textField.setText("");

    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JComboBox comboBox = new JComboBox(rows[0]);
    comboBox.setMaximumRowCount(4);/*from w w  w.jav  a  2  s .  c  o m*/

    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("JTable Anatomy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(new DefaultTableModel(rows, headers));

    table.getColumnModel().getColumn(1).setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    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);/*  w ww .  j  a  v a2 s.  co m*/
        petBreed.requestFocusInWindow();
    });
    gui.add(petSpecies);
    gui.add(petBreed);

    JOptionPane.showMessageDialog(null, gui);
}