- 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 to display the items in a JComboBox in Java
The following is an example to display the first element in a JComboBox in Java:
Example
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); String[] strArr = new String[] { "Laptop", "Mobile", "Desktop", "Tablet" }; JComboBox<String> comboBox = new JComboBox<>(strArr); panel.add(comboBox, BorderLayout.NORTH); JTextArea text = new JTextArea(5, 5); panel.add(text, BorderLayout.CENTER); JButton btn = new JButton("Click"); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { text.setText((String) comboBox.getSelectedItem()); comboBox.setSelectedIndex(0); } }); panel.add(btn, BorderLayout.SOUTH); JFrame frame = new JFrame(); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
The output is as follows displaying the first item:
Output
Now, select the second item (Mobile) and press the “Click” button. The following will get displayed:
- Related Articles
- How to display the different font items inside a JComboBox in Java?
- How to center align the items of a JComboBox in Java?
- How to display the first element in a JComboBox in Java
- How to add items in a JComboBox on runtime in Java
- How can we sort the items of a JComboBox in Java?
- How can we set the border to JComboBox items in Java?
- How to hide and display JCombobox with a JCheckBox in Java?
- How can we set the foreground and background color to JComboBox items in Java?
- How to handle action event for JComboBox in Java?
- How to display the JList items from top to bottom and left to right in Java?
- Java Program to set JComboBox in JOptionPane
- How to pre-select JComboBox item by index in Java?
- How to create a Default Cell Editor that uses a JComboBox in Java?
- How can we implement editable JComboBox in Java?
- How to display all items in the list in the drop down in Selenium?

Advertisements