
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between a JComboBox and a JList in Java?
In Java, a JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.
JComboBox
A JComboBox can be editable or read-only. An ActionListener, ChangeListener, or ItemListener interface can be used to handle the user actions on a JComboBox.
Syntax
The following is the syntax for JComboBox initialization:
String[] fruits = {"Apple", "Banana", "Orange", "Mango"}; JComboBox<String> comboBox = new JComboBox<>(fruits);
We can create a JComboBox instance from an array or a vector. Most of the time, we will use ComboBoxModel to manipulate ComboBox's elements.
Methods
The following are some common methods in JComboBox
addItem()
This method adds an item to the item list. It works only if the JComboBox uses a mutable data model.
Method Declaration:
public void addItem?(an_Item item)
removeItem()
Removes an item from the item list. This method works only if the JComboBox uses a mutable data model.
Method Declaration:
public void removeItem?(Object an_Object)
getSelectedItem()
A getSelectedItem() method can be used to get the selected or entered item from a combo box.
Method Declaration:
public Object getSelectedItem()
setEditable()
A setEditable() method can be used to turn on or turn off the text input part of a combo box.
Method Declaration:
public void setEditable?(boolean Boolean_Value)
getItemCount()
The getItemCount() method returns the number of items in the list as an integer value.
Method Declaration:
public int getItemCount()
When to Use JComboBox
We can use the JComboBox in Java when:
- We need a drop-down list (like a form input).
- Only a single selection is required.
- Screen space is limited (e.g., in a toolbar or form).
- We want to allow custom text input (editable mode).
Example of JComboBox
Below is an example of a JComboBox representing a dropdown on a Swing GUI in Java:
import java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame { JComboBoxTest() { setTitle("JComboBox Test"); String country[] = {"India","Aus","Singapore","England","Newzealand"}; JComboBox jcb = new JComboBox(country); setLayout(new FlowLayout()); add(jcb); setSize(300, 250); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new JComboBoxTest(); } }
Output
JList
A JList is a component that allows the user to choose either a single selection or multiple selections. The JList class itself does not support a scrollbar. In order to add a scrollbar, we have to use the JScrollPane class together with the JList class.
Syntax
The following is the syntax for JList initialization:
String[] fruits = {"Apple", "Banana", "Orange", "Mango"}; JList<String> list = new JList<>(fruits);
The JScrollPane then manages a scrollbar automatically. A DefaultListModel class provides a simple implementation of a list model, which can be used to manage items displayed by a JList control.
Methods
The following are some common methods in JList
getSelectedIndex()
The getSelectedIndex() method returns the index of the first selected item or 1 if no items are selected.
Method Declaration:
public int getSelectedIndex()
getSelectedIndices()
The getSelectedIndices method returns an array with the index of each selected item. The array is empty if no items are selected.
Method Declaration:
public int[] getSelectedIndices()
getSelectedValue()
The getSelectedValue() method returns the first selected item or null if no items are selected.
Method Declaration:
public E getSelectedValue()
setSelectionMode()
Sets selection mode as SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, or MULTIPLE_INTERVAL_SELECTION.
Method Declaration:
public void setSelectionMode(int selectionMode)
When to Use JList
We can use the JList in Java when:
- We need multiple selections.
- We want all options visible at once.
- The list items have relationships (e.g., file lists, playlist items).
- We need better control over selection behavior.
Example of JList
Below is an example of a JComboBox representing a list on a Swing GUI in Java:
import java.awt.*; import javax.swing.*; public class JListTest extends JFrame { JListTest() { setTitle("JList Test"); DefaultListModel dlm = new DefaultListModel(); dlm.addElement("India"); dlm.addElement("Aus"); dlm.addElement("England"); dlm.addElement("Singapore"); JList list = new JList(); list.setModel(dlm); setLayout(new FlowLayout()); add(list); setSize(350,275); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JListTest(); } }
Output
Difference Table
The following are the key differences between a JComboBox and a JList in Java:
Criteria | JComboBox | JList |
---|---|---|
Display Style | Drop-down (shows only selected item when closed) | Always visible list (may need scrolling) |
Selection Mode | Single selection only | Single, multiple, or range selection |
Editability | Can be made editable (custom input allowed) | Not editable |
Space Usage | Compact (good for forms) | Takes more space (shows all items) |
Data Model | ComboBoxModel | ListModel |