- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we sort the items of a JComboBox in Java?
A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener, and ItemListener interfaces when the user actions on a combo box. By default, a JComboBox does not support for sorting the items, we can customize the code by extending the DefaultComboBoxModel class.
Example
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class JComboBoxSorterTest extends JFrame { private JComboBox comboBox; private JTextField textField; public JComboBoxSorterTest() { setTitle("JComboBoxSorter Test"); setLayout(new FlowLayout()); textField = new JTextField(10); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { comboBox.addItem(textField.getText()); textField.setText(""); comboBox.showPopup(); } }); String[] items = {"raja", "archana", "vineet", "krishna", "adithya"}; SortedComboBoxModel model = new SortedComboBoxModel(items); comboBox = new JComboBox(model); comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); Box box1 = Box.createHorizontalBox(); box1.add(new JLabel("Enter a name and hit enter ")); box1.add(textField); Box box2 = Box.createHorizontalBox(); box2.add(comboBox); add(box1); add(box2); setSize(375, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } // Customize the code for sorting of items in the JComboBox private class SortedComboBoxModel extends DefaultComboBoxModel { public SortedComboBoxModel() { super(); } public SortedComboBoxModel(Object[] items) { Arrays.sort(items); int size = items.length; for (int i = 0; i < size; i++) { super.addElement(items[i]); } setSelectedItem(items[0]); } public SortedComboBoxModel(Vector items) { Collections.sort(items); int size = items.size(); for (int i = 0; i < size; i++) { super.addElement(items.elementAt(i)); } setSelectedItem(items.elementAt(0)); } public void addElement(Object element) { insertElementAt(element, 0); } public void insertElementAt(Object element, int index) { int size = getSize(); for (index = 0; index < size; index++) { Comparable c = (Comparable) getElementAt(index); if (c.compareTo(element) > 0) { break; } } super.insertElementAt(element, index); } } public static void main(String[] args) { new JComboBoxSorterTest(); } }
Output
- Related Articles
- How can we set the border to JComboBox items in Java?
- How can we set the foreground and background color to JComboBox items in Java?
- How can we implement editable JComboBox in Java?
- How to center align the items of a JComboBox in Java?
- How to display the items in a JComboBox in Java
- How can we implement auto-complete JComboBox in Java?\n
- Can we disable JComboBox arrow button in Java?
- How to display the different font items inside a JComboBox in Java?
- How to add items in a JComboBox on runtime in Java
- How can we sort a JSONArray in Java?
- How can we sort a JSONObject in Java?\n
- How can we show a popup menu when the user right-clicks on a JComboBox in Java?
- Sort items in a Java TreeSet
- How can we sort a JTable on a particular column in Java?
- Can we sort a list with Lambda in Java?

Advertisements