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