JComboBox class in java

JComboBox class

The JComboBox class is used to create the combobox (drop-down list). At a time only one item can be selected from the item list.

Constructors

  • JComboBox()
  • JComboBox(Object[] items)
  • JComboBox(Vector<?> items)

Methods

  • public void addItem(Object anObject)
  • public void removeItem(Object anObject)
  • public void removeAllItems()
  • public void setEditable(boolean b)
  • public void addActionListener(ActionListener a)
  • public void addItemListener(ItemListener i)
Example
import javax.swing.*;
public class test
{
JFrame f;
test()
{
f=new JFrame(“My ComboBox”);
String country[]={“Aus”,”India”,”U.S.A”,”England”,”Japan”};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args)
{
new test();
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *