All for Joomla All for Webmasters

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.

Commonly used Constructors of JComboBox class:

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

Commonly used methods of JComboBox class:

1) public void addItem(Object anObject): is used to add an item to the item list.
2) public void removeItem(Object anObject): is used to delete an item to the item list.
3) public void removeAllItems(): is used to remove all the items from the list.
4) public void setEditable(boolean b): is used to determine whether the JComboBox is editable.
5) public void addActionListener(ActionListener a): is used to add the ActionListener.
6) public void addItemListener(ItemListener i): is used to add the ItemListener.

Example of JComboBox class:

  1. import javax.swing.*;
  2. public class Combo {
  3. JFrame f;
  4. Combo(){
  5.     f=new JFrame(“Combo ex”);
  6.     String country[]={“India”,“Aus”,“U.S.A”,“England”,“Newzeland”};
  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 Combo();
  16. }
  17. }
submit to reddit

Leave a Reply