List of usage examples for javax.swing JComboBox JComboBox
public JComboBox(Vector<E> items)
JComboBox
that contains the elements in the specified Vector. From source file:Main.java
public static void main(String[] args) { Integer[] numbers = { 1, 2, 3 }; String[] names = { "A", "B", "C" }; JComboBox numberCombo = new JComboBox(numbers); JComboBox nameCombo = new JComboBox(names); JPanel p = new JPanel(new GridLayout(0, 1, 3, 3)); p.add(numberCombo);/* w w w. j a v a 2 s . com*/ p.add(nameCombo); JOptionPane.showMessageDialog(null, p); Integer chosenNumber = (Integer) numberCombo.getSelectedItem(); System.out.println("Chosen Number: " + chosenNumber); String chosenName = (String) nameCombo.getSelectedItem(); System.out.println("Chosen Name: " + chosenName); }
From source file:Main.java
public static void main(String[] args) throws Exception { Object[] options = { "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "None of the above" }; JComboBox optionList = new JComboBox(options); optionList.setSelectedIndex(7);/*w w w . j a v a 2s.c o m*/ JOptionPane.showMessageDialog(null, optionList, "Title", JOptionPane.QUESTION_MESSAGE); }
From source file:Main.java
public static void main(String[] args) { JComboBox<String> comboBox = new JComboBox<>(new String[] { "A", "B", "C" }); comboBox.setEditable(true);// w w w . j a v a 2s . c o m JTextField editorComponent = (JTextField) comboBox.getEditor().getEditorComponent(); editorComponent.addActionListener(e -> { editorComponent.transferFocus(); }); JPanel panel = new JPanel(new FlowLayout()); panel.add(new JLabel("Field 1")); panel.add(comboBox); panel.add(new JLabel("Field 2")); panel.add(new JTextField(10)); panel.add(new JLabel("Field 3")); panel.add(new JTextField(10)); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); Container c = frame.getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox = new JComboBox<>(new String[] { "Something", "Stuff", "Beep" }); JButton add = new JButton("Add item"); add.addActionListener(new ActionListener() { @Override//from www . j a v a2 s .co m public void actionPerformed(ActionEvent e) { comboBox.addItem("Item"); } }); frame.add(comboBox); frame.add(add, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 400);//w w w . j a v a 2s . co m frame.setVisible(true); String[] list = { "1", "2", "3", "4", }; JComboBox<String> comb = new JComboBox<>(list); final JPopupMenu pop = new JPopupMenu(); pop.add(comb); frame.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { System.out.println("mousePressed"); pop.show(e.getComponent(), e.getX(), e.getY()); } }); }
From source file:Main.java
public static void main(String[] args) { String items[] = { "Item1", "item1" }; JFrame f = new JFrame(); JPanel panel = new JPanel(); JComboBox<String> combo = new JComboBox<>(items); combo.setEditable(true);// ww w. j a v a2s .c o m JTextField txt = new JTextField(10); panel.add(combo); panel.add(txt); f.add(panel); combo.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent ie) { String str = (String) combo.getSelectedItem(); txt.setText(str); } }); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400, 100); f.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; JComboBox<String> comboBox = new JComboBox<>(labels); comboBox.setEditable(true);// w w w.j ava 2 s. com comboBox.addActionListener(new ActionListener() { boolean found = false; @Override public void actionPerformed(ActionEvent actionEvent) { String s = (String) comboBox.getSelectedItem(); for (int i = 0; i < comboBox.getItemCount(); i++) { if (comboBox.getItemAt(i).toString().equals(s)) { found = true; break; } } if (!found) { System.out.println("Added: " + s); comboBox.addItem(s); } found = false; } }); frame.add(comboBox); frame.pack(); frame.setVisible(true); }
From source file:SelectingComboSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox comboBox = new JComboBox(labels); frame.add(comboBox, BorderLayout.SOUTH); JComboBox.KeySelectionManager manager = new JComboBox.KeySelectionManager() { public int selectionForKey(char aKey, ComboBoxModel aModel) { System.out.println(aKey); return -1; }/*from ww w. ja va 2 s . c om*/ }; comboBox.setKeySelectionManager(manager); 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("Editable JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(labels); comboBox.setMaximumRowCount(5);//from w ww. ja v a 2s.com comboBox.setEditable(true); frame.add(comboBox, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + comboBox.getSelectedItem()); System.out.println(", Position: " + comboBox.getSelectedIndex()); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); 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 w w w . jav a2 s . c om*/ 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); }