Saturday, August 24, 2013

JComboBox

JComboBox represents a combination of text field and a drop down list. You can select a single item from drop down list. In default, a combo box is not editable. An editable field can be made by using the setEditable(boolean editable) method. Below are the constructors and useful methods of the JComboBox class.

Constructors of the JComboBox class:
-JComboBox(Object[] items)
creates a combo box that its items are taken from an array.
-JComboBox(ComboBoxModel model)
creates a combo box that its items are taken from a model.
-JComboBox(Vector<?> items)
creates a combo box that its items are taken from a vector.

Useful methods of the JComboBox class:
-addItem(Object item):void
adds an item to the combo box.
-getItemAt(int index):Object
gets the item at the specified index.
-getItemCount():int
gets the number of items in the combo box.
-getSelectedIndex():int
gets index of the selected item.
-getSelectedItem():Object
gets the selected item object.
-removeAllItems():void
removes all items in the combo box.
-removeItem(Object item):void
remove the specified item from the combo box.
-removeItemAt(int index): void
removes the item at the specified index from the combo box.
-setSelectedIndex(int index):void
selects the item at the specified index.
-setSelectedItem(Object):void
selects the specified item object.

Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class JComboBoxShow extends JFrame implements ItemListener{
JLabel lbl;
JComboBoxShow(String title){
setTitle(title);
setSize(400,300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new FlowLayout());
String[] lans={"English","Khmer","Chinese","French","Russian","Japanese"};
JComboBox cb=new JComboBox(lans);
cb.addItemListener(this);
JPanel p=new JPanel();
p.setAlignmentY(JPanel.LEFT_ALIGNMENT);
p.setLayout(new GridLayout(3,1));
lbl=new JLabel();
lbl.setForeground(Color.RED);
p.add(new JLabel("Select your language:"));
p.add(cb);
p.add(lbl);
contpane.add(p);
setVisible(true);
}

public void itemStateChanged(ItemEvent e){
JComboBox cb=(JComboBox)e.getSource();
lbl.setText("Selected language: "+cb.getSelectedItem());
}

}

public class JFrameSwing {
public static void main(String[] args){
new JComboBoxShow ("JComboBox");
}
}

JComboBox Swing

In the example above, an array of string languages are defined. This array acts as a data source of the JComboBox component and it is supplied to the JComboBox when its object is created.  To perform an action when the item of the JComboBox is selected, you need to register the ItemListener with it and then override the itemStateChanged method. Simply, when an item of the JComboBox is selected, its text displays in the JLabel, lbl.

No comments:

Post a Comment