List of usage examples for javax.swing JComboBox getSelectedItem
public Object getSelectedItem()
From source file:Main.java
public static void main(String[] args) throws Exception { int SIZE = 14; String FONT = "Dialog"; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane tp = new JTextPane(); tp.setFont(new Font(FONT, Font.PLAIN, SIZE)); tp.setPreferredSize(new Dimension(400, 300)); StyledDocument doc = tp.getStyledDocument(); Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style boldStyle = doc.addStyle("bold", defaultStyle); StyleConstants.setBold(boldStyle, true); String boldText = "this is bold test"; String plainText = "this is plain."; doc.insertString(doc.getLength(), boldText, boldStyle); doc.insertString(doc.getLength(), plainText, defaultStyle); JPanel panel = new JPanel(); panel.add(tp);/* w ww . j a v a 2 s.com*/ JComboBox<String> zoomCombo = new JComboBox<String>( new String[] { "0.75", "1.00", "1.50", "1.75", "2.00" }); zoomCombo.addActionListener(e -> { String s = (String) zoomCombo.getSelectedItem(); double scale = new Double(s).doubleValue(); int size = (int) (SIZE * scale); tp.setFont(new Font(FONT, Font.PLAIN, size)); }); zoomCombo.setSelectedItem("1.00"); JPanel optionsPanel = new JPanel(); optionsPanel.add(zoomCombo); panel.setBackground(Color.WHITE); frame.add(panel, BorderLayout.CENTER); frame.add(optionsPanel, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); }
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 ww . ja v a 2 s. c om 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:ColorComboBox.java
public static void main(String args[]) { Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; JFrame frame = new JFrame("Color JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); final JComboBox comboBox = new JComboBox(colors); comboBox.setMaximumRowCount(5);/*w ww. j a v a2 s . co m*/ comboBox.setEditable(true); comboBox.setRenderer(new ColorCellRenderer()); Color color = (Color) comboBox.getSelectedItem(); ComboBoxEditor editor = new ColorComboBoxEditor(color); comboBox.setEditor(editor); contentPane.add(comboBox, BorderLayout.NORTH); final JLabel label = new JLabel(); label.setOpaque(true); label.setBackground((Color) comboBox.getSelectedItem()); contentPane.add(label, BorderLayout.CENTER); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color selectedColor = (Color) comboBox.getSelectedItem(); label.setBackground(selectedColor); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); frame.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);/*from w ww.ja v a2s. c o m*/ 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: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 ww w.j av a 2s .c o m 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) { String items[] = { "Item1", "item1" }; JFrame f = new JFrame(); JPanel panel = new JPanel(); JComboBox<String> combo = new JComboBox<>(items); combo.setEditable(true);/*from w w w.jav a2 s .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 window = new JFrame("Example"); JPanel contentPane = new JPanel(); contentPane.setLayout(new GridBagLayout()); JComboBox comboBoxfields = new JComboBox(COMBO_BOX_ELEMENTS); comboBoxfields.setFont(new Font("sansserif", Font.TRUETYPE_FONT | Font.PLAIN, 15)); comboBoxfields.setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); comboBoxfields.setMaximumRowCount(5); comboBoxfields.addActionListener(//from w w w . j a v a 2 s . c om e -> System.out.println("'" + comboBoxfields.getSelectedItem().toString() + "'" + " was selected")); contentPane.add(comboBoxfields); window.add(contentPane); window.setSize(500, 500); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); }
From source file:Main.java
public static void main(String args[]) throws Exception { Color colors[] = { Color.GREEN, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW }; JFrame frame = new JFrame("Color JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(colors); comboBox.setMaximumRowCount(5);/* w w w. j a va2 s . c o m*/ comboBox.setEditable(true); comboBox.setRenderer(new ColorCellRenderer()); frame.add(comboBox, BorderLayout.NORTH); final JLabel label = new JLabel(); label.setOpaque(true); label.setBackground((Color) comboBox.getSelectedItem()); frame.add(label, BorderLayout.CENTER); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color selectedColor = (Color) comboBox.getSelectedItem(); label.setBackground(selectedColor); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); 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//from w w w.j a v a2s. c o m 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(String[] args) { ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js"); String[] ops = { "+", "-", "*", "/" }; JPanel gui = new JPanel(new BorderLayout(2, 2)); JPanel labels = new JPanel(new GridLayout(0, 1)); gui.add(labels, BorderLayout.WEST); labels.add(new JLabel("a")); labels.add(new JLabel("operand")); labels.add(new JLabel("b")); labels.add(new JLabel("=")); JPanel controls = new JPanel(new GridLayout(0, 1)); gui.add(controls, BorderLayout.CENTER); JTextField a = new JTextField(10); controls.add(a);//from ww w. j a va 2 s. c o m JComboBox operand = new JComboBox(ops); controls.add(operand); JTextField b = new JTextField(10); controls.add(b); JTextField output = new JTextField(10); controls.add(output); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { String expression = a.getText() + operand.getSelectedItem() + b.getText(); try { Object result = engine.eval(expression); if (result == null) { output.setText("Output was 'null'"); } else { output.setText(result.toString()); } } catch (ScriptException se) { output.setText(se.getMessage()); } } }; operand.addActionListener(al); a.addActionListener(al); b.addActionListener(al); JOptionPane.showMessageDialog(null, gui); }