Java Swing JComboBox
Java Swing JComboBox
JComboBox is a part of Java Swing package.JComboBox displays a drop down list.It shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only.To create a ComboBox,we can use JComboBox class.
Constructors used with JComboBox
Type of Constructor | Constructor Description |
---|---|
JComboBox() | It creates an empty JComboBox. |
JComboBox(ComboBoxModel m) | It creates a new JComboBox with items from specified ComboBoxModel. |
JComboBox(Object[] items) | It creates a JComboBox that contains the elements in the specified array. |
JComboBox(Vector items) | It creates a new JComboBox with items from the specified vector. |
The following example create an object of class JComboBox.
Example
Basic Methods of JComboBox class :
Method Name | Description |
---|---|
void addItem(Object anObject) | It adds the items to the item list. |
getItemAt(int i) | returns the item at index i. |
getSelectedItem() | It returns the item which is selected. |
removeItemAt(int i) | It removes the element at index i. |
void removeAllItems() | It is used to remove all the items from the list. |
void addActionListener(ActionListener a) | It is used to add the ActionListener. |
void setEditable(boolean b) | It is used to determine whether the JComboBox is editable. |
void addItemListener(ItemListener i) | It is used to add the ItemListener. |
isPopupVisible() | It determines the visibility of the popup. |
getItemCount() | It returns the number of items from the list. |
Example of JComboBox with JComboBox(Object[] arr)
import javax.swing.*;
class ComboBoxEx extends JFrame
{
JComboBox cb;
JLabel lbl;
public ComboBoxEx()
{
String mobile[]={"SAMSUNG","APPLE","VIVO","OPPO","One Plus","LENOVO","NOKIA"};
JComboBox cb=new JComboBox(mobile);
lbl=new JLabel("Select Brand:");
lbl.setBounds(50, 50, 200, 50);
cb.setBounds(50, 100,150,20);
add(cb);
add(lbl);
setLayout(null);
setSize(300,300);
setTitle("Combo Box Example: ");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class ComboBoxExample
{
public static void main(String[] args)
{
ComboBoxEx combo=new ComboBoxEx();
}
}
output
The code above will display :
Example of Java Swing JComboBox with ActionListener
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class ComboBoxEx extends JFrame implements ActionListener
{
JComboBox cb;
JLabel lbl,lbl2;
JButton btn;
public ComboBoxEx()
{
String course[]={"C","C++","Java","Python","JavaScript","PHP"};
cb=new JComboBox(course);
lbl=new JLabel("Select Course:");
lbl2=new JLabel();
btn=new JButton("Click");
lbl.setBounds(50, 50, 200, 50);
cb.setBounds(50, 100,150,20);
lbl2.setBounds(250, 50, 200, 50);
btn.setBounds(250, 100, 100, 30);
add(cb);
add(lbl);
add(lbl2);
add(btn);
btn.addActionListener(this);
setLayout(null);
setSize(400,300);
setTitle("Combo Box Example: ");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Click")
{
String str;
str=cb.getSelectedItem().toString();
JOptionPane.showMessageDialog(null,"Programming language Selected is : " +str);
}
}
}
public class ComboBoxExample
{
public static void main(String[] args)
{
ComboBoxEx combo=new ComboBoxEx();
}
}
Output
The code above will display :
Example of Java Swing JComboBox using DefaultComboBoxModel<String> with ItemListener
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class ComboBoxEx extends JFrame implements ItemListener
{
JComboBox cb;
JLabel lbl,lbl2;
public ComboBoxEx()
{
DefaultComboBoxModel<String> course= new DefaultComboBoxModel<String>();
course.addElement("C");
course.addElement("C++");
course.addElement("Java");
course.addElement("Python");
course.addElement("JavaScript");
course.addElement("PHP");
cb=new JComboBox(course);
lbl=new JLabel("Select Course:");
lbl2=new JLabel();
lbl.setBounds(50, 50, 200, 50);
cb.setBounds(50, 100,150,20);
lbl2.setBounds(200, 50, 200, 50);
cb.setMaximumRowCount(5);
cb.setEditable(true);
add(cb);
add(lbl);
add(lbl2);
cb.addItemListener(this);
setLayout(null);
setSize(400,300);
setTitle("Combo Box Example: ");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ae)
{
if(ae.getSource()==cb)
{
lbl2.setText("You have selected "+cb.getItemAt(cb.getSelectedIndex()) );
}
}
}
public class ComboBoxExample
{
public static void main(String[] args)
{
ComboBoxEx combo=new ComboBoxEx();
}
}
Output
The code above will display :
Example to add items in a JComboBox from JTextField and performing different operations on it.
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ComboBoxExample extends JFrame implements ActionListener{
JTextField txt1;
JComboBox com;
JButton btnadd,btnshow,btnrem,btnremall,btnindex;
Container c;
JPanel p1,p2,p3;
public ComboBoxExample()
{
setLayout(null);
txt1= new JTextField();
com = new JComboBox();
btnadd = new JButton("Add");
btnshow = new JButton("Show");
btnrem = new JButton("Remove");
btnremall = new JButton("Remove All");
btnindex = new JButton("Show Index");
c = getContentPane();
c.setLayout(new FlowLayout());
c.setVisible(true);
p1= new JPanel(new GridLayout(1, 2));
p2=new JPanel(new GridLayout(5,1));
p3= new JPanel(new GridLayout(5, 1));
btnadd.addActionListener(this);
btnshow.addActionListener(this);
btnrem.addActionListener(this);
btnremall.addActionListener(this);
btnindex.addActionListener(this);
p2.add(txt1);
p2.add(com);
p3.add(btnadd);
p3.add(btnshow);
p3.add(btnindex);
p3.add(btnrem);
p3.add(btnremall);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1.add(p2);
p1.add(p3);
c.add(p1);
}
public void actionPerformed(ActionEvent e)
{
String str = txt1.getText().trim();
if(e.getActionCommand()=="Add")
{
com.addItem(str);
txt1.setText("");
txt1.requestFocus();
}
else if(e.getActionCommand()=="Show")
{
JOptionPane.showMessageDialog(rootPane,"You Have Selected:"+com.getSelectedItem().toString());
}
else if(e.getActionCommand()=="Remove")
{
int i= com.getSelectedIndex();
com.removeItemAt(i);
}
else if(e.getActionCommand()=="Remove All")
{
com.removeAllItems();
}
else if(e.getActionCommand()=="Show Index")
{
Integer i=com.getSelectedIndex();
txt1.setText(i.toString());
}
}
public static void main(String []args)
{
ComboBoxExample combobox = new ComboBoxExample();
combobox.setVisible(true);
combobox.setSize(400, 400);
}
}
Output
The code above will display :