Monday, 13 April 2015

JComboBox class

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()
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.          
7.        String country[]={"India","Aus","U.S.A","England","Newzeland"};  
8.          
9.        JComboBox cb=new JComboBox(country);  
10.     cb.setBounds(5050,90,20);  
11.     f.add(cb);  
12.       
13.     f.setLayout(null);  
14.     f.setSize(400,500);  
15.     f.setVisible(true);  
16.       
17. }  
18. public static void main(String[] args) {  
19.     new Combo();  
20.       
21. }  

22. }  

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...