Option Panes

advertisement
Option Panes
CSCI 201L
Jeffrey Miller, Ph.D.
HTTP://WWW-SCF.USC.EDU/~CSCI201
USC CSCI 201L
Outline
▪ Option Panes
▪ Program
USC CSCI 201L
2/30
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
Option Panes
USC CSCI 201L
3/30
Message Dialog Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
javax.swing.ImageIcon;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JOptionPane;
public class Test extends JFrame {
public Test() {
super("JOptionPane Example");
JButton jb = new JButton("Click Here");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(Test.this,
“Message",
“Message Dialog",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("uscshield.png"));
}
});
add(jb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 225);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}
Option Panes
USC CSCI 201L
4/30
Message Dialog Types
JOptionPane.showMessageDialog(Test.this,
"Message",
"Message Dialog",
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(Test.this,
"Message",
"Message Dialog",
JOptionPane.QUESTION_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);
Option Panes
USC CSCI 201L
5/30
Confirmation Dialog Example
1
2
3
4
5
6
7
8
9
10
11
12
import
import
import
import
import
public class Test extends JFrame {
public Test() {
super("JOptionPane Example");
JButton jb = new JButton("Click Here");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JOptionPane;
int selection = JOptionPane.showConfirmDialog(Test.this, "Please confirm.", "Confirmation", JOptionPane.YES_NO_OPTION);
switch (selection) {
case JOptionPane.YES_OPTION: // case JOptionPane.OK_OPTION is the same
System.out.println("Yes");
break;
case JOptionPane.NO_OPTION:
System.out.println("No");
break;
case JOptionPane.CANCEL_OPTION:
System.out.println("Cancel");
break;
case JOptionPane.CLOSED_OPTION:
System.out.println("Closed");
break;
}
}
});
add(jb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 225);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}
Option Panes
USC CSCI 201L
6/30
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);
Option Panes
USC CSCI 201L
7/30
Input Dialog Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame {
public Test() {
super("JOptionPane Example");
JButton jb = new JButton("Click Here");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String value = JOptionPane.showInputDialog(Test.this,
"Input",
"Input Dialog",
JOptionPane.QUESTION_MESSAGE);
System.out.println("value = " + value);
if (value == null) {
System.out.println("Cancel button or X clicked");
}
}
});
add(jb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 225);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}
Option Panes
USC CSCI 201L
8/30
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);
Option Panes
USC CSCI 201L
9/30
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);
Option Panes
USC CSCI 201L
10/30
Option Dialog Example
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame {
public Test() {
super("JOptionPane Example");
JButton jb = new JButton("Click Here");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Object [] options = {"Yes please", "No thanks", "Maybe"};
int value = JOptionPane.showOptionDialog(Test.this,
"Which option would you like?",
"Option Dialog",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, // icon to display
options,
options[1]);
System.out.println("value = " + value);
if (value == 0) {
System.out.println("Yes please clicked");
}
else if (value == 1) {
System.out.println("No thanks clicked");
}
else if (value == 2) {
System.out.println("Maybe clicked");
}
else { // value == -1
System.out.println("X clicked");
}
}
});
add(jb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 225);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}
Option Panes
USC CSCI 201L
11/30
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
Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Option Panes
USC CSCI 201L
12/30
Outline
▪ Option Panes
▪ Program
USC CSCI 201L
13/30
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.
Image from http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Program
USC CSCI 201L
14/30
Download