Presentation is loading. Please wait.

Presentation is loading. Please wait.

Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Similar presentations


Presentation on theme: "Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

1 Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

2 Outline USC CSCI 201L2/30 ▪O▪Option Panes ▪P▪Program

3 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 201L3/30 Option Panes

4 Message Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.ImageIcon; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JOptionPane; 8 9 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); 23 24 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 201L4/30 Option Panes

5 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 201L5/30 Option Panes JOptionPane.showMessageDialog(Test.this, "Message", "Message Dialog", JOptionPane.QUESTION_MESSAGE);

6 Confirmation Dialog Example 1 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; 6 7 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 same 16 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); 31 32 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 201L6/30 Option Panes

7 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 201L7/30 Option Panes

8 Input Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JButton; 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 8 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 }); 24 25 add(jb); 26 27 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 201L8/30 Option Panes

9 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 201L9/30 Option Panes

10 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 201L10/30 Option Panes

11 Option Dialog Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JButton; 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 8 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 display 21 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 == -1 34 System.out.println("X clicked"); 35 } 36 } 37 }); 38 39 add(jb); 40 41 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 201L11/30 Option Panes

12 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 201L12/30 Option Panes Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

13 Outline USC CSCI 201L13/30 ▪O▪Option Panes ▪P▪Program

14 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 201L14/30 Program Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html


Download ppt "Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."

Similar presentations


Ads by Google