

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java Program to set JComboBox in JOptionPane
Let us first crate a ComboBox and add items to it −
Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; JComboBox comboBox = new JComboBox(sports);
Now, add the combo box to the JOptionPane −
JOptionPane.showMessageDialog(null, comboBox, "Fav Sports", JOptionPane.QUESTION_MESSAGE);
The following is an example to set JComboBox in JOptionPane −
Example
package my; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) throws Exception { JPanel panel = new JPanel(new GridBagLayout()); Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; JComboBox comboBox = new JComboBox(sports); comboBox.setSelectedIndex(1); JOptionPane.showMessageDialog(null, comboBox, "Fav Sports", JOptionPane.QUESTION_MESSAGE); panel.add(comboBox); } }
Output
Be default, we have set index i.e. “Cricket”. You can select any of the options now from the ComboBox set −
- Related Questions & Answers
- Java Program to set an icon for JOptionPane
- Can we set JOptionPane with predefined selection in Java?
- How can we set the border to JComboBox items in Java?
- Java Program to disable the first item on a JComboBox
- How can we set the foreground and background color to JComboBox items in Java?
- How to use JOptionPane with Array Elements in Java?
- How to handle action event for JComboBox in Java?
- How to display the items in a JComboBox in Java
- Can we disable JComboBox arrow button in Java?
- How can we implement editable JComboBox in Java?
- How to pre-select JComboBox item by index in Java?
- Java Program to set title position in Java
- How to add items in a JComboBox on runtime in Java
- How to display the first element in a JComboBox in Java
- What are the different types of JOptionPane dialogs in Java?
Advertisements