Java Swing How to - Create dialog for JComboBox using JOptionPane








Question

We would like to know how to create dialog for JComboBox using JOptionPane.

Answer

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
// w  w w. j av a  2s  .c  om
public class Main {
  public static void main(String[] args) {
    String[] weekdays = { "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday" };
    final JComboBox<String> combo = new JComboBox<>(weekdays);

    String[] options = { "OK", "Cancel", "Help" };

    String title = "Title";
    int selection = JOptionPane.showOptionDialog(null, combo, title,
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options,
        options[0]);

    if (selection > 0) {
      System.out.println("selection is: " + options[selection]);
    }

    Object weekday = combo.getSelectedItem();
    if (weekday != null) {
      System.out.println("weekday: " + weekday);
    }

  }
}