Java Swing How to - Show JComboBox in JOptionPane








Question

We would like to know how to show JComboBox in JOptionPane.

Answer

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
// w  w w .java 2s .  c  om
public class Main {

  public static void main(String[] args) throws Exception {
    Object[] options = { "Option 1", "Option 2", "Option 3", "Option 4",
        "Option 5", "Option 6", "Option 7", "None of the above" };
    JComboBox optionList = new JComboBox(options);
    optionList.setSelectedIndex(7);
    JOptionPane.showMessageDialog(null, optionList, "Title",
        JOptionPane.QUESTION_MESSAGE);
  }
}