KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > OptionPaneDemo


1 /*
2  * @(#)OptionPaneDemo.java 1.11 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)OptionPaneDemo.java 1.11 05/11/17
39  */

40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58 /**
59  * JOptionPaneDemo
60  *
61  * @version 1.11 11/17/05
62  * @author Jeff Dinkins
63  */

64 public class OptionPaneDemo extends DemoModule {
65
66     /**
67      * main method allows us to run as a standalone demo.
68      */

69     public static void main(String JavaDoc[] args) {
70     OptionPaneDemo demo = new OptionPaneDemo(null);
71     demo.mainImpl();
72     }
73
74     /**
75      * OptionPaneDemo Constructor
76      */

77     public OptionPaneDemo(SwingSet2 swingset) {
78     // Set the title for this demo, and an icon used to represent this
79
// demo inside the SwingSet2 app.
80
super(swingset, "OptionPaneDemo", "toolbar/JOptionPane.gif");
81
82     JPanel demo = getDemoPanel();
83
84     demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS));
85
86     JPanel bp = new JPanel() {
87         public Dimension getMaximumSize() {
88         return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
89         }
90     };
91     bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));
92
93     bp.add(Box.createRigidArea(VGAP30));
94     bp.add(Box.createRigidArea(VGAP30));
95
96     bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15));
97     bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15));
98     bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15));
99     bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15));
100     bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue());
101
102     demo.add(Box.createHorizontalGlue());
103     demo.add(bp);
104     demo.add(Box.createHorizontalGlue());
105     }
106
107     public JButton createWarningDialogButton() {
108     Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) {
109         public void actionPerformed(ActionEvent e) {
110         JOptionPane.showMessageDialog(
111             getDemoPanel(),
112             getString("OptionPaneDemo.warningtext"),
113             getString("OptionPaneDemo.warningtitle"),
114             JOptionPane.WARNING_MESSAGE
115         );
116         }
117     };
118     return createButton(a);
119     }
120
121     public JButton createMessageDialogButton() {
122     Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) {
123         URL img = getClass().getResource("/resources/images/optionpane/bottle.gif");
124         String JavaDoc imagesrc = "<img SRC=\"" + img + "\" width=\"284\" height=\"100\">";
125         String JavaDoc message = getString("OptionPaneDemo.messagetext");
126         public void actionPerformed(ActionEvent e) {
127         JOptionPane.showMessageDialog(
128             getDemoPanel(),
129             "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>"
130         );
131         }
132     };
133     return createButton(a);
134     }
135
136     public JButton createConfirmDialogButton() {
137     Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) {
138         public void actionPerformed(ActionEvent e) {
139                 int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion"));
140                 if(result == JOptionPane.YES_OPTION) {
141             JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes"));
142         } else if(result == JOptionPane.NO_OPTION) {
143                     JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno"));
144         }
145         }
146     };
147     return createButton(a);
148     }
149
150     public JButton createInputDialogButton() {
151     Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) {
152         public void actionPerformed(ActionEvent e) {
153                 String JavaDoc result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));
154                 if ((result != null) && (result.length() > 0)) {
155                     JOptionPane.showMessageDialog(getDemoPanel(),
156                                     result + ": " +
157                                     getString("OptionPaneDemo.inputresponse"));
158                 }
159         }
160     };
161     return createButton(a);
162     }
163
164     public JButton createComponentDialogButton() {
165     Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {
166         public void actionPerformed(ActionEvent e) {
167         // In a ComponentDialog, you can show as many message components and
168
// as many options as you want:
169

170         // Messages
171
Object JavaDoc[] message = new Object JavaDoc[4];
172                 message[0] = getString("OptionPaneDemo.componentmessage");
173                 message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));
174
175                 JComboBox cb = new JComboBox();
176                 cb.addItem(getString("OptionPaneDemo.component_cb1"));
177                 cb.addItem(getString("OptionPaneDemo.component_cb2"));
178                 cb.addItem(getString("OptionPaneDemo.component_cb3"));
179                 message[2] = cb;
180
181                 message[3] = getString("OptionPaneDemo.componentmessage2");
182
183         // Options
184
String JavaDoc[] options = {
185             getString("OptionPaneDemo.component_op1"),
186             getString("OptionPaneDemo.component_op2"),
187             getString("OptionPaneDemo.component_op3"),
188             getString("OptionPaneDemo.component_op4"),
189             getString("OptionPaneDemo.component_op5")
190         };
191                 int result = JOptionPane.showOptionDialog(
192             getDemoPanel(), // the parent that the dialog blocks
193
message, // the dialog message array
194
getString("OptionPaneDemo.componenttitle"), // the title of the dialog window
195
JOptionPane.DEFAULT_OPTION, // option type
196
JOptionPane.INFORMATION_MESSAGE, // message type
197
null, // optional icon, use null to use the default icon
198
options, // options string array, will be made into buttons
199
options[3] // option that should be made into a default button
200
);
201         switch(result) {
202            case 0: // yes
203
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));
204              break;
205            case 1: // no
206
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));
207              break;
208            case 2: // maybe
209
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));
210              break;
211            case 3: // probably
212
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));
213              break;
214            default:
215              break;
216         }
217
218         }
219     };
220     return createButton(a);
221     }
222         
223     public JButton createButton(Action a) {
224     JButton b = new JButton() {
225         public Dimension getMaximumSize() {
226         int width = Short.MAX_VALUE;
227         int height = super.getMaximumSize().height;
228         return new Dimension(width, height);
229         }
230     };
231     // setting the following client property informs the button to show
232
// the action text as it's name. The default is to not show the
233
// action text.
234
b.putClientProperty("displayActionText", Boolean.TRUE);
235     b.setAction(a);
236     // b.setAlignmentX(JButton.CENTER_ALIGNMENT);
237
return b;
238     }
239
240 }
241
Popular Tags