1 | /**
|
---|
2 | *
|
---|
3 | */
|
---|
4 | package test;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.beans.PropertyChangeEvent;
|
---|
8 | import java.beans.PropertyChangeListener;
|
---|
9 |
|
---|
10 | import javax.swing.JDialog;
|
---|
11 | import javax.swing.JFrame;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JTextField;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @author cdaller
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public class DialogTest {
|
---|
21 |
|
---|
22 | public void openDialog(JFrame frame) {
|
---|
23 |
|
---|
24 | JTextField textField = new JTextField(10);
|
---|
25 |
|
---|
26 | //Create an array of the text and components to be displayed.
|
---|
27 | String msgString1 = "What was Dr. SEUSS's real last name?";
|
---|
28 | String msgString2 = "(The answer is .)";
|
---|
29 | Object[] array = {msgString1, msgString2, textField};
|
---|
30 |
|
---|
31 | //Create an array specifying the number of dialog buttons
|
---|
32 | //and their text.
|
---|
33 | Object[] options = {"button1", "button2"};
|
---|
34 |
|
---|
35 | //Create the JOptionPane.
|
---|
36 | final JOptionPane optionPane = new JOptionPane(array,
|
---|
37 | JOptionPane.QUESTION_MESSAGE,
|
---|
38 | JOptionPane.YES_NO_OPTION,
|
---|
39 | null,
|
---|
40 | options,
|
---|
41 | options[0]);
|
---|
42 |
|
---|
43 | // final JOptionPane optionPane = new JOptionPane("The only way to close this dialog is by\n"
|
---|
44 | // + "pressing one of the following buttons.\n" + "Do you understand?",
|
---|
45 | // JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
|
---|
46 |
|
---|
47 |
|
---|
48 | final JDialog dialog = new JDialog(frame, "Click a button", true);
|
---|
49 | DialogClosingThread closer = new DialogClosingThread(dialog);
|
---|
50 | closer.observe(textField);
|
---|
51 | dialog.setContentPane(optionPane);
|
---|
52 | optionPane.addPropertyChangeListener(new PropertyChangeListener() {
|
---|
53 | public void propertyChange(PropertyChangeEvent e) {
|
---|
54 | String prop = e.getPropertyName();
|
---|
55 |
|
---|
56 | if (dialog.isVisible() && (e.getSource() == optionPane)
|
---|
57 | && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
|
---|
58 | // If you were going to check something
|
---|
59 | // before closing the window, you'd do
|
---|
60 | // it here.
|
---|
61 | dialog.setVisible(false);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | });
|
---|
65 | closer.start();
|
---|
66 | dialog.pack();
|
---|
67 | dialog.setVisible(true);
|
---|
68 |
|
---|
69 |
|
---|
70 | System.out.println("value: " + optionPane.getValue());
|
---|
71 |
|
---|
72 | // int value = ((Integer) optionPane.getValue()).intValue();
|
---|
73 | // if (value == JOptionPane.YES_OPTION) {
|
---|
74 | // System.out.println("yes");
|
---|
75 | // } else if (value == JOptionPane.NO_OPTION) {
|
---|
76 | // System.out.println("no");
|
---|
77 | // }
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static void main(String[] args) {
|
---|
82 | //1. Create the frame.
|
---|
83 | JFrame frame = new JFrame("FrameDemo");
|
---|
84 |
|
---|
85 | //2. Optional: What happens when the frame closes?
|
---|
86 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
87 |
|
---|
88 | //3. Create components and put them in the frame.
|
---|
89 | //...create emptyLabel...
|
---|
90 | frame.getContentPane().add(new JLabel("test"), BorderLayout.CENTER);
|
---|
91 |
|
---|
92 | //4. Size the frame.
|
---|
93 | frame.pack();
|
---|
94 | frame.setSize(600,400);
|
---|
95 | frame.setLocation(0,0);
|
---|
96 |
|
---|
97 | //5. Show it.
|
---|
98 | frame.setVisible(true);
|
---|
99 | new DialogTest().openDialog(frame);
|
---|
100 | }
|
---|
101 | }
|
---|