Option PanesCSCI 201L
Jeffrey Miller, Ph.D.
HTTP://WWW-SCF.USC.EDU/~CSCI201
USC CSCI 201L
Outline
USC CSCI 201L 2/30
▪ Option Panes▪ Program
Option Panes
▪ Option panes are popup dialog boxes▪ There are four types of option panes in Java
› Message dialog – shows a message and waits for the user to click OK
› Confirmation dialog – shows a question and asks for confirmation, such as OK or Cancel
› Input dialog – shows a question and gets the user’s input from a text field, combo box, or list
› Option dialog – shows a question and gets the user’s answer from a set of options
▪ To display an option pane, there are static methods on the JOptionPane class called showMessageDialog, showConfirmationDialog, showInputDialog, and showOptionDialog
USC CSCI 201L 3/30Option Panes
Message Dialog Example1 import java.awt.event.ActionEvent;2 import java.awt.event.ActionListener;34 import javax.swing.ImageIcon;5 import javax.swing.JButton;6 import javax.swing.JFrame;7 import javax.swing.JOptionPane;89 public class Test extends JFrame {10 public Test() {11 super("JOptionPane Example");12 JButton jb = new JButton("Click Here");13 jb.addActionListener(new ActionListener() {14 public void actionPerformed(ActionEvent ae) {15 JOptionPane.showMessageDialog(Test.this, 16 “Message", 17 “Message Dialog", 18 JOptionPane.INFORMATION_MESSAGE, 19 new ImageIcon("uscshield.png"));20 }21 });22 add(jb);2324 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);25 setSize(250, 225);26 setLocationRelativeTo(null);27 setVisible(true);28 }29 30 public static void main(String args[]) {31 Test t = new Test();32 }33 }
USC CSCI 201L 4/30Option Panes
Message Dialog Types
JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.WARNING_MESSAGE);
USC CSCI 201L 5/30Option Panes
JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.QUESTION_MESSAGE);
Confirmation Dialog Example1 import java.awt.event.ActionEvent;2 import java.awt.event.ActionListener;3 import javax.swing.JButton;4 import javax.swing.JFrame;5 import javax.swing.JOptionPane;67 public class Test extends JFrame {8 public Test() {9 super("JOptionPane Example");10 JButton jb = new JButton("Click Here");11 jb.addActionListener(new ActionListener() {12 public void actionPerformed(ActionEvent ae) {
13 int selection = JOptionPane.showConfirmDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_OPTION);14 switch (selection) {15 case JOptionPane.YES_OPTION: // case JOptionPane.OK_OPTION is the same16 System.out.println("Yes");17 break;18 case JOptionPane.NO_OPTION:19 System.out.println("No");20 break;21 case JOptionPane.CANCEL_OPTION:22 System.out.println("Cancel");23 break;24 case JOptionPane.CLOSED_OPTION:25 System.out.println("Closed");26 break;27 }28 }29 });30 add(jb);3132 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);33 setSize(250, 225);34 setLocationRelativeTo(null);35 setVisible(true);36 }37 38 public static void main(String args[]) {39 Test t = new Test();40 }41 }
USC CSCI 201L 6/30Option Panes
Confirmation Dialog Types
JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_OPTION);
JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
JOptionPane.showConfirmationDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.OK_CANCEL_OPTION);
USC CSCI 201L 7/30Option Panes
Input Dialog Example1 import java.awt.event.ActionEvent;2 import java.awt.event.ActionListener;34 import javax.swing.JButton;5 import javax.swing.JFrame;6 import javax.swing.JOptionPane;78 public class Test extends JFrame {9 public Test() {10 super("JOptionPane Example");11 JButton jb = new JButton("Click Here");12 jb.addActionListener(new ActionListener() {13 public void actionPerformed(ActionEvent ae) {14 String value = JOptionPane.showInputDialog(Test.this, 15 "Input", 16 "Input Dialog", 17 JOptionPane.QUESTION_MESSAGE);18 System.out.println("value = " + value);19 if (value == null) {20 System.out.println("Cancel button or X clicked");21 }22 }23 });2425 add(jb);2627 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);28 setSize(250, 225);29 setLocationRelativeTo(null);30 setVisible(true);31 }32 33 public static void main(String args[]) {34 Test t = new Test();35 }36 }
USC CSCI 201L 8/30Option Panes
Input Dialog Types
String value = JOptionPane.showInputDialog(Test.this, “Input", “Input Dialog", JOptionPane.QUESTION_MESSAGE);
Object values[] = {"option 1", "option 2", "option 3"}; Object defaultValue = "option 2"; Object value = JOptionPane.showInputDialog(Test.this, "Input", "Input Dialog", JOptionPane.QUESTION_MESSAGE, null, // icon values, defaultValue);
USC CSCI 201L 9/30Option Panes
Input Dialog Types (cont.)
▪ If you have more 20 or more items in the list, you will have a multiple selection box instead of a drop down list
Object values[] = new Object[20]; for (int i=0; i < 20; i++) { values[i] = “option “ + (i+1); } Object defaultValue = "option 2"; Object value = JOptionPane.showInputDialog(Test.this, "Input", "Input Dialog", JOptionPane.QUESTION_MESSAGE, null, // icon values, defaultValue);
USC CSCI 201L 10/30Option Panes
Option Dialog Example1 import java.awt.event.ActionEvent;2 import java.awt.event.ActionListener;34 import javax.swing.JButton;5 import javax.swing.JFrame;6 import javax.swing.JOptionPane;78 public class Test extends JFrame {9 public Test() {10 super("JOptionPane Example");11 JButton jb = new JButton("Click Here");12 jb.addActionListener(new ActionListener() {13 public void actionPerformed(ActionEvent ae) {
14 Object [] options = {"Yes please", "No thanks", "Maybe"};15 int value = JOptionPane.showOptionDialog(Test.this, 16 "Which option would you like?", 17 "Option Dialog",18 JOptionPane.YES_NO_CANCEL_OPTION,19 JOptionPane.QUESTION_MESSAGE,20 null, // icon to display21 options,22 options[1]);23 System.out.println("value = " + value);24 if (value == 0) {25 System.out.println("Yes please clicked");26 }27 else if (value == 1) {28 System.out.println("No thanks clicked");29 }30 else if (value == 2) {31 System.out.println("Maybe clicked");32 }33 else { // value == -134 System.out.println("X clicked");35 }36 }37 });3839 add(jb);4041 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);42 setSize(250, 225);43 setLocationRelativeTo(null);44 setVisible(true);45 }46 47 public static void main(String args[]) {48 Test t = new Test();49 }50 }
USC CSCI 201L 11/30Option Panes
Custom Option Panes
▪ In most cases, the standard option panes will suffice for an application
▪ If the standard option panes are too restrictive, you can always create your own using JDialog
USC CSCI 201L 12/30Option Panes
Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Outline
USC CSCI 201L 13/30
▪ Option Panes▪ Program
Program
▪ Create a dialog box that pops up over an application that looks like the following. Retrieve the values of the radio buttons and the button when it is clicked.
USC CSCI 201L 14/30Program
Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html