1
36
37
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
64 public class OptionPaneDemo extends DemoModule {
65
66
69 public static void main(String[] args) {
70 OptionPaneDemo demo = new OptionPaneDemo(null);
71 demo.mainImpl();
72 }
73
74
77 public OptionPaneDemo(SwingSet2 swingset) {
78 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 imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">";
125 String 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 result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));
154 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.inputresponse"));
155 }
156 };
157 return createButton(a);
158 }
159
160 public JButton createComponentDialogButton() {
161 Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {
162 public void actionPerformed(ActionEvent e) {
163
166 Object[] message = new Object[4];
168 message[0] = getString("OptionPaneDemo.componentmessage");
169 message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));
170
171 JComboBox cb = new JComboBox();
172 cb.addItem(getString("OptionPaneDemo.component_cb1"));
173 cb.addItem(getString("OptionPaneDemo.component_cb2"));
174 cb.addItem(getString("OptionPaneDemo.component_cb3"));
175 message[2] = cb;
176
177 message[3] = getString("OptionPaneDemo.componentmessage2");
178
179 String[] options = {
181 getString("OptionPaneDemo.component_op1"),
182 getString("OptionPaneDemo.component_op2"),
183 getString("OptionPaneDemo.component_op3"),
184 getString("OptionPaneDemo.component_op4"),
185 getString("OptionPaneDemo.component_op5")
186 };
187 int result = JOptionPane.showOptionDialog(
188 getDemoPanel(), message, getString("OptionPaneDemo.componenttitle"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[3] );
197 switch(result) {
198 case 0: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));
200 break;
201 case 1: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));
203 break;
204 case 2: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));
206 break;
207 case 3: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));
209 break;
210 default:
211 break;
212 }
213
214 }
215 };
216 return createButton(a);
217 }
218
219 public JButton createButton(Action a) {
220 JButton b = new JButton() {
221 public Dimension getMaximumSize() {
222 int width = Short.MAX_VALUE;
223 int height = super.getMaximumSize().height;
224 return new Dimension(width, height);
225 }
226 };
227 b.putClientProperty("displayActionText", Boolean.TRUE);
231 b.setAction(a);
232 return b;
234 }
235
236 }
237