Java Jcombobox example set selected item actionlistener  getselecteditem

JComboBox is Drop down box with multiple values in Java Swing.

It is defined as javax.swing.JComboBox<E>.

Here E is type of element of JComboBox.

JComboBox Constructors

Sr NoConstructors and Description
1JComboBox()
Create a JComboBox with default default data model
2JComboBox(ComboBoxModel aModel)
Create a JComboBox with ComboBoxModel aModel
3JComboBox(E[] items)
Create a JComboBox with specified array
4JComboBox(Vector items)
Create a JComboBox with specified Vector

Methods of JComboBox

Sr NoMethods and Description
1void addItem(E item)
Add an Item to item list
2Object getSelectedItem()
Returns the selected Item
3void removeItem(Object anObject)
Remove an Item from Item list
4void removeItem(Object anObject)
Remove an Item from Item list
5void addActionListener(ActionListener l)
Add action listener
6getItemAt(int index)
Get Item at specified index

JComboBox add and show items

  1. Create a frame with any size.
  2. Create Reference for Container, JLables and JComboBox.
  3. get content page and set its layout null.
  4. Create JLabels and JComboBox objects.
  5. Add elements to JComboBox using addItem() and by using String[]
  6. Set bounds for JLables and JComboBox
  7. Add all Components to container.

Examples of JComboBox

JComboBox add and show items
Fig: JComboBox add and show items

JComboBox actionlistener get selected item and get selected index

jComboBox1.addActionListener(this); is used to add action listener to jComboBox1.

You have to override actionPerformed(ActionEvent e) to handle combo box event.

combo.getSelectedItem() displays the selected item

combo.getSelectedIndex() is used to display selected item index.

JComboBox indexing starts from 0.

jcombobox get selected item example

JComboBox actionlistener get selected item and get selected index
Fig: JComboBox actionlistener get selected item and get selected index

JComboBox set selected item and set selected index

  1. create JComboBox
  2. To select specific item based on name use comboBoxRef.setSelectedItem("Item Name");
  3. To select specific item based on index use comboBoxRef.setSelectedIndex(3);
  4. Index number must be between 0 to itemSize-1
JComboBox set selected item and set selected index
Fig JComboBox set selected item and set selected index

jcombobox get selected item using ItemListener

JComboBox Example ItemListener

  1. Create JComboBox
  2. Add ItemListner to this
  3. override itemStateChanged(ItemEvent e) method
  4. get Item Event Source from combobox
  5. select item and index by using getSelectedItem() and getSelectedIndex()
JComboBox ItemListener Example
Fig: JComboBox ItemListener Example

JComboBox Show Image in combo box using ListCellRenderer and ItemListner

JComboBox Show Image in combo box using ListCellRenderer and ItemListner
Fig: Show image in JComboBox

How to use combobox in java netbeans

We have developed many examples in netbeans.

It is a very simple copy and above example and paste in netbeans/ eclipse then run.

Run a swing program is same as simple java program

How to use combobox in java netbeans
Fig: How to use combobox in java netbeans
JCombobox Example in netbeans
Fig: JCombobox Example in Netbeans

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Registration Form in Java Swing
  6. Login form in Java Swing
  7. Simple Calculator in Java Applet
  8. Applet Life Cycle in Java

Image Reference

https://freeicons.io/indian-republic-day-icons/india-gate-indian%20republic%20day-icon-7661#

Taj Mahal icon icon by Icons8

https://thenounproject.com/term/victoria-memorial/2165511/