Java Swing How to - Let combine showInputDialog and showOptionDialog in JOptionPane








Question

We would like to know how to let combine showInputDialog and showOptionDialog in JOptionPane.

Answer

import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/* w w  w . java  2  s. c o  m*/
public class Main {
  public static void main(String[] args) {
    Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    Object[] possibilities = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Integer i = (Integer) JOptionPane.showOptionDialog(null, null,
        "ShowInputDialog", JOptionPane.PLAIN_MESSAGE, 1, errorIcon,
        possibilities, 0);

    // or

    Integer ii = (Integer) JOptionPane.showInputDialog(null,
        "Select number:\n\from JComboBox", "ShowInputDialog",
        JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");

  }
}