In this example we are going to see how to use JComboBox Get Selected Items In Java.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; public class Main { public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton jButton1 = new JButton("Button"); String[] mystring = { "BMW", "Audi", "Toyota", "Honda" }; final JList jList1 = new JList(mystring); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { Object contents = jList1.getSelectedValue(); System.out.println(contents); } }); frame.add(jList1, "Center"); frame.add(jButton1, "South"); frame.setSize(300, 200); frame.setVisible(true); } } |
Output:
1 2 3 4 | Toyota Honda |