Java JComboBox

Java JComboBox

The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class.

JComboBox class declaration

Let’s see the declaration for javax.swing.JComboBox class.

  1. public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible

Commonly used Constructors:

Constructor Description
JComboBox() Creates a JComboBox with a default data model.
JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the specified Vector.

Commonly used Methods:

Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from the list.
void setEditable(boolean b) It is used to determine whether the JComboBox is editable.
void addActionListener(ActionListener a) It is used to add the ActionListener.
void addItemListener(ItemListener i) It is used to add the ItemListener.

 

Java JComboBox Example

  1. import javax.swing.*;
  2. public class ComboBoxExample {
  3. JFrame f;
  4. ComboBoxExample(){
  5.     f=new JFrame(“ComboBox Example”);
  6.     String country[]={“India”,“Aus”,“U.S.A”,“England”,“Newzealand”};
  7.     JComboBox cb=new JComboBox(country);
  8.     cb.setBounds(5050,90,20);
  9.     f.add(cb);
  10.     f.setLayout(null);
  11.     f.setSize(400,500);
  12.     f.setVisible(true);
  13. }
  14. public static void main(String[] args) {
  15.     new ComboBoxExample();
  16. }
  17. }

Output:

JAVA Jcombobox 1


Java JComboBox Example with ActionListener

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class ComboBoxExample {
  4. JFrame f;
  5. ComboBoxExample(){
  6.     f=new JFrame(“ComboBox Example”);
  7.     final JLabel label = new JLabel();
  8.     label.setHorizontalAlignment(JLabel.CENTER);
  9.     label.setSize(400,100);
  10.     JButton b=new JButton(“Show”);
  11.     b.setBounds(200,100,75,20);
  12.     String languages[]={“C”,“C++”,“C#”,“Java”,“PHP”};
  13.     final JComboBox cb=new JComboBox(languages);
  14.     cb.setBounds(50100,90,20);
  15.     f.add(cb); f.add(label); f.add(b);
  16.     f.setLayout(null);
  17.     f.setSize(350,350);
  18.     f.setVisible(true);
  19.     b.addActionListener(new ActionListener() {
  20.         public void actionPerformed(ActionEvent e) {
  21. String data = “Programming language Selected: “
  22.    + cb.getItemAt(cb.getSelectedIndex());
  23. label.setText(data);
  24. }
  25. });
  26. }
  27. public static void main(String[] args) {
  28.     new ComboBoxExample();
  29. }
  30. }

Output:

JAVA Jcombobox 2

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java JComboBox The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class. JComboBox class declaration Let’s see the declaration for javax.swing.JComboBox class. public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible Commonly used Constructors: Constructor Description JComboBox() Creates a JComboBox with a default data […]

Tags:

....
...