Java Swing How to - Use JOptionPane with Array Elements








Question

We would like to know how to use JOptionPane with Array Elements.

Answer

import javax.swing.JOptionPane;
/*from ww  w.  ja  v a2 s  .  c o m*/
public class Main {

  public static void main(String[] args) {
    String[] possibilities = { "Adult Ticket", "Childs Ticket",
        "Over Fifty Ticket" };
    String s = (String) JOptionPane.showInputDialog(null,
        "What ticket would you like to buy?", "Ticket Dialog",
        JOptionPane.PLAIN_MESSAGE, null, possibilities, possibilities[0]);
    switch (s) {
    case "Adult Ticket":
      System.out.println("Buy Adult");
      break;
    case "Childs Ticket":
      System.out.println("Child Adult");
      break;
    case "Over Fifty Ticket":
      System.out.println("Over Fifty Adult");
      break;
    }
  }
}