1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing; 27 28 import java.awt.BorderLayout; 29 import java.awt.Component; 30 import java.awt.Container; 31 import java.awt.Dialog; 32 import java.awt.Dimension; 33 import java.awt.KeyboardFocusManager; 34 import java.awt.Frame; 35 import java.awt.Point; 36 import java.awt.HeadlessException; 37 import java.awt.Window; 38 import java.beans.PropertyChangeEvent; 39 import java.beans.PropertyChangeListener; 40 import java.awt.event.WindowListener; 41 import java.awt.event.WindowAdapter; 42 import java.awt.event.WindowEvent; 43 import java.awt.event.ComponentAdapter; 44 import java.awt.event.ComponentEvent; 45 import java.io.IOException; 46 import java.io.InvalidObjectException; 47 import java.io.ObjectInputStream; 48 import java.io.ObjectOutputStream; 49 import java.io.Serializable; 50 import java.util.Vector; 51 import javax.swing.plaf.OptionPaneUI; 52 import javax.swing.event.InternalFrameEvent; 53 import javax.swing.event.InternalFrameAdapter; 54 import javax.accessibility.*; 55 import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP; 56 import sun.awt.AWTAccessor; 57 58 /** 59 * <code>JOptionPane</code> makes it easy to pop up a standard dialog box that 60 * prompts users for a value or informs them of something. 61 * For information about using <code>JOptionPane</code>, see 62 * <a 63 href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html">How to Make Dialogs</a>, 64 * a section in <em>The Java Tutorial</em>. 65 * 66 * <p> 67 * 68 * While the <code>JOptionPane</code> 69 * class may appear complex because of the large number of methods, almost 70 * all uses of this class are one-line calls to one of the static 71 * <code>showXxxDialog</code> methods shown below: 72 * <blockquote> 73 * 74 * 75 * <table border=1 summary="Common JOptionPane method names and their descriptions"> 76 * <tr> 77 * <th>Method Name</th> 78 * <th>Description</th> 79 * </tr> 80 * <tr> 81 * <td>showConfirmDialog</td> 82 * <td>Asks a confirming question, like yes/no/cancel.</td> 83 * </tr> 84 * <tr> 85 * <td>showInputDialog</td> 86 * <td>Prompt for some input.</td> 87 * </tr> 88 * <tr> 89 * <td>showMessageDialog</td> 90 * <td>Tell the user about something that has happened.</td> 91 * </tr> 92 * <tr> 93 * <td>showOptionDialog</td> 94 * <td>The Grand Unification of the above three.</td> 95 * </tr> 96 * </table> 97 * 98 * </blockquote> 99 * Each of these methods also comes in a <code>showInternalXXX</code> 100 * flavor, which uses an internal frame to hold the dialog box (see 101 * {@link JInternalFrame}). 102 * Multiple convenience methods have also been defined -- overloaded 103 * versions of the basic methods that use different parameter lists. 104 * <p> 105 * All dialogs are modal. Each <code>showXxxDialog</code> method blocks 106 * the caller until the user's interaction is complete. 107 * 108 * <table cellspacing=6 cellpadding=4 border=0 style="float:right" summary="layout"> 109 * <tr> 110 * <td style="background-color:#FFe0d0" rowspan=2>icon</td> 111 * <td style="background-color:#FFe0d0">message</td> 112 * </tr> 113 * <tr> 114 * <td style="background-color:#FFe0d0">input value</td> 115 * </tr> 116 * <tr> 117 * <td style="background-color:#FFe0d0" colspan=2>option buttons</td> 118 * </tr> 119 * </table> 120 * 121 * The basic appearance of one of these dialog boxes is generally 122 * similar to the picture at the right, although the various 123 * look-and-feels are 124 * ultimately responsible for the final result. In particular, the 125 * look-and-feels will adjust the layout to accommodate the option pane's 126 * <code>ComponentOrientation</code> property. 127 * <br style="clear:all"> 128 * <p> 129 * <b>Parameters:</b><br> 130 * The parameters to these methods follow consistent patterns: 131 * <blockquote> 132 * <dl> 133 * <dt>parentComponent<dd> 134 * Defines the <code>Component</code> that is to be the parent of this 135 * dialog box. 136 * It is used in two ways: the <code>Frame</code> that contains 137 * it is used as the <code>Frame</code> 138 * parent for the dialog box, and its screen coordinates are used in 139 * the placement of the dialog box. In general, the dialog box is placed 140 * just below the component. This parameter may be <code>null</code>, 141 * in which case a default <code>Frame</code> is used as the parent, 142 * and the dialog will be 143 * centered on the screen (depending on the {@literal L&F}). 144 * <dt><a name=message>message</a><dd> 145 * A descriptive message to be placed in the dialog box. 146 * In the most common usage, message is just a <code>String</code> or 147 * <code>String</code> constant. 148 * However, the type of this parameter is actually <code>Object</code>. Its 149 * interpretation depends on its type: 150 * <dl> 151 * <dt>Object[]<dd>An array of objects is interpreted as a series of 152 * messages (one per object) arranged in a vertical stack. 153 * The interpretation is recursive -- each object in the 154 * array is interpreted according to its type. 155 * <dt>Component<dd>The <code>Component</code> is displayed in the dialog. 156 * <dt>Icon<dd>The <code>Icon</code> is wrapped in a <code>JLabel</code> 157 * and displayed in the dialog. 158 * <dt>others<dd>The object is converted to a <code>String</code> by calling 159 * its <code>toString</code> method. The result is wrapped in a 160 * <code>JLabel</code> and displayed. 161 * </dl> 162 * <dt>messageType<dd>Defines the style of the message. The Look and Feel 163 * manager may lay out the dialog differently depending on this value, and 164 * will often provide a default icon. The possible values are: 165 * <ul> 166 * <li><code>ERROR_MESSAGE</code> 167 * <li><code>INFORMATION_MESSAGE</code> 168 * <li><code>WARNING_MESSAGE</code> 169 * <li><code>QUESTION_MESSAGE</code> 170 * <li><code>PLAIN_MESSAGE</code> 171 * </ul> 172 * <dt>optionType<dd>Defines the set of option buttons that appear at 173 * the bottom of the dialog box: 174 * <ul> 175 * <li><code>DEFAULT_OPTION</code> 176 * <li><code>YES_NO_OPTION</code> 177 * <li><code>YES_NO_CANCEL_OPTION</code> 178 * <li><code>OK_CANCEL_OPTION</code> 179 * </ul> 180 * You aren't limited to this set of option buttons. You can provide any 181 * buttons you want using the options parameter. 182 * <dt>options<dd>A more detailed description of the set of option buttons 183 * that will appear at the bottom of the dialog box. 184 * The usual value for the options parameter is an array of 185 * <code>String</code>s. But 186 * the parameter type is an array of <code>Objects</code>. 187 * A button is created for each object depending on its type: 188 * <dl> 189 * <dt>Component<dd>The component is added to the button row directly. 190 * <dt>Icon<dd>A <code>JButton</code> is created with this as its label. 191 * <dt>other<dd>The <code>Object</code> is converted to a string using its 192 * <code>toString</code> method and the result is used to 193 * label a <code>JButton</code>. 194 * </dl> 195 * <dt>icon<dd>A decorative icon to be placed in the dialog box. A default 196 * value for this is determined by the <code>messageType</code> parameter. 197 * <dt>title<dd>The title for the dialog box. 198 * <dt>initialValue<dd>The default selection (input value). 199 * </dl> 200 * </blockquote> 201 * <p> 202 * When the selection is changed, <code>setValue</code> is invoked, 203 * which generates a <code>PropertyChangeEvent</code>. 204 * <p> 205 * If a <code>JOptionPane</code> has configured to all input 206 * <code>setWantsInput</code> 207 * the bound property <code>JOptionPane.INPUT_VALUE_PROPERTY</code> 208 * can also be listened 209 * to, to determine when the user has input or selected a value. 210 * <p> 211 * When one of the <code>showXxxDialog</code> methods returns an integer, 212 * the possible values are: 213 * <ul> 214 * <li><code>YES_OPTION</code> 215 * <li><code>NO_OPTION</code> 216 * <li><code>CANCEL_OPTION</code> 217 * <li><code>OK_OPTION</code> 218 * <li><code>CLOSED_OPTION</code> 219 * </ul> 220 * <b>Examples:</b> 221 * <dl> 222 * <dt>Show an error dialog that displays the message, 'alert': 223 * <dd><code> 224 * JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE); 225 * </code> 226 * <dt>Show an internal information dialog with the message, 'information': 227 * <dd><pre> 228 * JOptionPane.showInternalMessageDialog(frame, "information", 229 * "information", JOptionPane.INFORMATION_MESSAGE); 230 * </pre> 231 * <dt>Show an information panel with the options yes/no and message 'choose one': 232 * <dd><pre>JOptionPane.showConfirmDialog(null, 233 * "choose one", "choose one", JOptionPane.YES_NO_OPTION); 234 * </pre> 235 * <dt>Show an internal information dialog with the options yes/no/cancel and 236 * message 'please choose one' and title information: 237 * <dd><pre>JOptionPane.showInternalConfirmDialog(frame, 238 * "please choose one", "information", 239 * JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); 240 * </pre> 241 * <dt>Show a warning dialog with the options OK, CANCEL, title 'Warning', and 242 * message 'Click OK to continue': 243 * <dd><pre> 244 * Object[] options = { "OK", "CANCEL" }; 245 * JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", 246 * JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 247 * null, options, options[0]); 248 * </pre> 249 * <dt>Show a dialog asking the user to type in a String: 250 * <dd><code> 251 * String inputValue = JOptionPane.showInputDialog("Please input a value"); 252 * </code> 253 * <dt>Show a dialog asking the user to select a String: 254 * <dd><pre> 255 * Object[] possibleValues = { "First", "Second", "Third" };<br> 256 * Object selectedValue = JOptionPane.showInputDialog(null, 257 * "Choose one", "Input", 258 * JOptionPane.INFORMATION_MESSAGE, null, 259 * possibleValues, possibleValues[0]); 260 * </pre> 261 * </dl> 262 * <b>Direct Use:</b><br> 263 * To create and use an <code>JOptionPane</code> directly, the 264 * standard pattern is roughly as follows: 265 * <pre> 266 * JOptionPane pane = new JOptionPane(<i>arguments</i>); 267 * pane.set<i>.Xxxx(...); // Configure</i> 268 * JDialog dialog = pane.createDialog(<i>parentComponent, title</i>); 269 * dialog.show(); 270 * Object selectedValue = pane.getValue(); 271 * if(selectedValue == null) 272 * return CLOSED_OPTION; 273 * <i>//If there is <b>not</b> an array of option buttons:</i> 274 * if(options == null) { 275 * if(selectedValue instanceof Integer) 276 * return ((Integer)selectedValue).intValue(); 277 * return CLOSED_OPTION; 278 * } 279 * <i>//If there is an array of option buttons:</i> 280 * for(int counter = 0, maxCounter = options.length; 281 * counter < maxCounter; counter++) { 282 * if(options[counter].equals(selectedValue)) 283 * return counter; 284 * } 285 * return CLOSED_OPTION; 286 * </pre> 287 * <p> 288 * <strong>Warning:</strong> Swing is not thread safe. For more 289 * information see <a 290 * href="package-summary.html#threading">Swing's Threading 291 * Policy</a>. 292 * <p> 293 * <strong>Warning:</strong> 294 * Serialized objects of this class will not be compatible with 295 * future Swing releases. The current serialization support is 296 * appropriate for short term storage or RMI between applications running 297 * the same version of Swing. As of 1.4, support for long term storage 298 * of all JavaBeans™ 299 * has been added to the <code>java.beans</code> package. 300 * Please see {@link java.beans.XMLEncoder}. 301 * 302 * @see JInternalFrame 303 * 304 * @beaninfo 305 * attribute: isContainer true 306 * description: A component which implements standard dialog box controls. 307 * 308 * @author James Gosling 309 * @author Scott Violet 310 * @since 1.2 311 */ 312 @SuppressWarnings("serial") // Same-version serialization only 313 public class JOptionPane extends JComponent implements Accessible 314 { 315 /** 316 * @see #getUIClassID 317 * @see #readObject 318 */ 319 private static final String uiClassID = "OptionPaneUI"; 320 321 /** 322 * Indicates that the user has not yet selected a value. 323 */ 324 public static final Object UNINITIALIZED_VALUE = "uninitializedValue"; 325 326 // 327 // Option types 328 // 329 330 /** 331 * Type meaning Look and Feel should not supply any options -- only 332 * use the options from the <code>JOptionPane</code>. 333 */ 334 public static final int DEFAULT_OPTION = -1; 335 /** Type used for <code>showConfirmDialog</code>. */ 336 public static final int YES_NO_OPTION = 0; 337 /** Type used for <code>showConfirmDialog</code>. */ 338 public static final int YES_NO_CANCEL_OPTION = 1; 339 /** Type used for <code>showConfirmDialog</code>. */ 340 public static final int OK_CANCEL_OPTION = 2; 341 342 // 343 // Return values. 344 // 345 /** Return value from class method if YES is chosen. */ 346 public static final int YES_OPTION = 0; 347 /** Return value from class method if NO is chosen. */ 348 public static final int NO_OPTION = 1; 349 /** Return value from class method if CANCEL is chosen. */ 350 public static final int CANCEL_OPTION = 2; 351 /** Return value form class method if OK is chosen. */ 352 public static final int OK_OPTION = 0; 353 /** Return value from class method if user closes window without selecting 354 * anything, more than likely this should be treated as either a 355 * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */ 356 public static final int CLOSED_OPTION = -1; 357 358 // 359 // Message types. Used by the UI to determine what icon to display, 360 // and possibly what behavior to give based on the type. 361 // 362 /** Used for error messages. */ 363 public static final int ERROR_MESSAGE = 0; 364 /** Used for information messages. */ 365 public static final int INFORMATION_MESSAGE = 1; 366 /** Used for warning messages. */ 367 public static final int WARNING_MESSAGE = 2; 368 /** Used for questions. */ 369 public static final int QUESTION_MESSAGE = 3; 370 /** No icon is used. */ 371 public static final int PLAIN_MESSAGE = -1; 372 373 /** Bound property name for <code>icon</code>. */ 374 public static final String ICON_PROPERTY = "icon"; 375 /** Bound property name for <code>message</code>. */ 376 public static final String MESSAGE_PROPERTY = "message"; 377 /** Bound property name for <code>value</code>. */ 378 public static final String VALUE_PROPERTY = "value"; 379 /** Bound property name for <code>option</code>. */ 380 public static final String OPTIONS_PROPERTY = "options"; 381 /** Bound property name for <code>initialValue</code>. */ 382 public static final String INITIAL_VALUE_PROPERTY = "initialValue"; 383 /** Bound property name for <code>type</code>. */ 384 public static final String MESSAGE_TYPE_PROPERTY = "messageType"; 385 /** Bound property name for <code>optionType</code>. */ 386 public static final String OPTION_TYPE_PROPERTY = "optionType"; 387 /** Bound property name for <code>selectionValues</code>. */ 388 public static final String SELECTION_VALUES_PROPERTY = "selectionValues"; 389 /** Bound property name for <code>initialSelectionValue</code>. */ 390 public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue"; 391 /** Bound property name for <code>inputValue</code>. */ 392 public static final String INPUT_VALUE_PROPERTY = "inputValue"; 393 /** Bound property name for <code>wantsInput</code>. */ 394 public static final String WANTS_INPUT_PROPERTY = "wantsInput"; 395 396 /** Icon used in pane. */ 397 transient protected Icon icon; 398 /** Message to display. */ 399 transient protected Object message; 400 /** Options to display to the user. */ 401 transient protected Object[] options; 402 /** Value that should be initially selected in <code>options</code>. */ 403 transient protected Object initialValue; 404 /** Message type. */ 405 protected int messageType; 406 /** 407 * Option type, one of <code>DEFAULT_OPTION</code>, 408 * <code>YES_NO_OPTION</code>, 409 * <code>YES_NO_CANCEL_OPTION</code> or 410 * <code>OK_CANCEL_OPTION</code>. 411 */ 412 protected int optionType; 413 /** Currently selected value, will be a valid option, or 414 * <code>UNINITIALIZED_VALUE</code> or <code>null</code>. */ 415 transient protected Object value; 416 /** Array of values the user can choose from. Look and feel will 417 * provide the UI component to choose this from. */ 418 protected transient Object[] selectionValues; 419 /** Value the user has input. */ 420 protected transient Object inputValue; 421 /** Initial value to select in <code>selectionValues</code>. */ 422 protected transient Object initialSelectionValue; 423 /** If true, a UI widget will be provided to the user to get input. */ 424 protected boolean wantsInput; 425 426 427 /** 428 * Shows a question-message dialog requesting input from the user. The 429 * dialog uses the default frame, which usually means it is centered on 430 * the screen. 431 * 432 * @param message the <code>Object</code> to display 433 * @exception HeadlessException if 434 * <code>GraphicsEnvironment.isHeadless</code> returns 435 * <code>true</code> 436 * @return user's input 437 * @see java.awt.GraphicsEnvironment#isHeadless 438 */ 439 public static String showInputDialog(Object message) 440 throws HeadlessException { 441 return showInputDialog(null, message); 442 } 443 444 /** 445 * Shows a question-message dialog requesting input from the user, with 446 * the input value initialized to <code>initialSelectionValue</code>. The 447 * dialog uses the default frame, which usually means it is centered on 448 * the screen. 449 * 450 * @param message the <code>Object</code> to display 451 * @param initialSelectionValue the value used to initialize the input 452 * field 453 * @return user's input 454 * @since 1.4 455 */ 456 public static String showInputDialog(Object message, Object initialSelectionValue) { 457 return showInputDialog(null, message, initialSelectionValue); 458 } 459 460 /** 461 * Shows a question-message dialog requesting input from the user 462 * parented to <code>parentComponent</code>. 463 * The dialog is displayed on top of the <code>Component</code>'s 464 * frame, and is usually positioned below the <code>Component</code>. 465 * 466 * @param parentComponent the parent <code>Component</code> for the 467 * dialog 468 * @param message the <code>Object</code> to display 469 * @exception HeadlessException if 470 * <code>GraphicsEnvironment.isHeadless</code> returns 471 * <code>true</code> 472 * @return user's input 473 * @see java.awt.GraphicsEnvironment#isHeadless 474 */ 475 public static String showInputDialog(Component parentComponent, 476 Object message) throws HeadlessException { 477 return showInputDialog(parentComponent, message, UIManager.getString( 478 "OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE); 479 } 480 481 /** 482 * Shows a question-message dialog requesting input from the user and 483 * parented to <code>parentComponent</code>. The input value will be 484 * initialized to <code>initialSelectionValue</code>. 485 * The dialog is displayed on top of the <code>Component</code>'s 486 * frame, and is usually positioned below the <code>Component</code>. 487 * 488 * @param parentComponent the parent <code>Component</code> for the 489 * dialog 490 * @param message the <code>Object</code> to display 491 * @param initialSelectionValue the value used to initialize the input 492 * field 493 * @return user's input 494 * @since 1.4 495 */ 496 public static String showInputDialog(Component parentComponent, Object message, 497 Object initialSelectionValue) { 498 return (String)showInputDialog(parentComponent, message, 499 UIManager.getString("OptionPane.inputDialogTitle", 500 parentComponent), QUESTION_MESSAGE, null, null, 501 initialSelectionValue); 502 } 503 504 /** 505 * Shows a dialog requesting input from the user parented to 506 * <code>parentComponent</code> with the dialog having the title 507 * <code>title</code> and message type <code>messageType</code>. 508 * 509 * @param parentComponent the parent <code>Component</code> for the 510 * dialog 511 * @param message the <code>Object</code> to display 512 * @param title the <code>String</code> to display in the dialog 513 * title bar 514 * @param messageType the type of message that is to be displayed: 515 * <code>ERROR_MESSAGE</code>, 516 * <code>INFORMATION_MESSAGE</code>, 517 * <code>WARNING_MESSAGE</code>, 518 * <code>QUESTION_MESSAGE</code>, 519 * or <code>PLAIN_MESSAGE</code> 520 * @return user's input 521 * @exception HeadlessException if 522 * <code>GraphicsEnvironment.isHeadless</code> returns 523 * <code>true</code> 524 * @see java.awt.GraphicsEnvironment#isHeadless 525 */ 526 public static String showInputDialog(Component parentComponent, 527 Object message, String title, int messageType) 528 throws HeadlessException { 529 return (String)showInputDialog(parentComponent, message, title, 530 messageType, null, null, null); 531 } 532 533 /** 534 * Prompts the user for input in a blocking dialog where the 535 * initial selection, possible selections, and all other options can 536 * be specified. The user will able to choose from 537 * <code>selectionValues</code>, where <code>null</code> implies the 538 * user can input 539 * whatever they wish, usually by means of a <code>JTextField</code>. 540 * <code>initialSelectionValue</code> is the initial value to prompt 541 * the user with. It is up to the UI to decide how best to represent 542 * the <code>selectionValues</code>, but usually a 543 * <code>JComboBox</code>, <code>JList</code>, or 544 * <code>JTextField</code> will be used. 545 * 546 * @param parentComponent the parent <code>Component</code> for the 547 * dialog 548 * @param message the <code>Object</code> to display 549 * @param title the <code>String</code> to display in the 550 * dialog title bar 551 * @param messageType the type of message to be displayed: 552 * <code>ERROR_MESSAGE</code>, 553 * <code>INFORMATION_MESSAGE</code>, 554 * <code>WARNING_MESSAGE</code>, 555 * <code>QUESTION_MESSAGE</code>, 556 * or <code>PLAIN_MESSAGE</code> 557 * @param icon the <code>Icon</code> image to display 558 * @param selectionValues an array of <code>Object</code>s that 559 * gives the possible selections 560 * @param initialSelectionValue the value used to initialize the input 561 * field 562 * @return user's input, or <code>null</code> meaning the user 563 * canceled the input 564 * @exception HeadlessException if 565 * <code>GraphicsEnvironment.isHeadless</code> returns 566 * <code>true</code> 567 * @see java.awt.GraphicsEnvironment#isHeadless 568 */ 569 public static Object showInputDialog(Component parentComponent, 570 Object message, String title, int messageType, Icon icon, 571 Object[] selectionValues, Object initialSelectionValue) 572 throws HeadlessException { 573 JOptionPane pane = new JOptionPane(message, messageType, 574 OK_CANCEL_OPTION, icon, 575 null, null); 576 577 pane.setWantsInput(true); 578 pane.setSelectionValues(selectionValues); 579 pane.setInitialSelectionValue(initialSelectionValue); 580 pane.setComponentOrientation(((parentComponent == null) ? 581 getRootFrame() : parentComponent).getComponentOrientation()); 582 583 int style = styleFromMessageType(messageType); 584 JDialog dialog = pane.createDialog(parentComponent, title, style); 585 586 pane.selectInitialValue(); 587 dialog.show(); 588 dialog.dispose(); 589 590 Object value = pane.getInputValue(); 591 592 if (value == UNINITIALIZED_VALUE) { 593 return null; 594 } 595 return value; 596 } 597 598 /** 599 * Brings up an information-message dialog titled "Message". 600 * 601 * @param parentComponent determines the <code>Frame</code> in 602 * which the dialog is displayed; if <code>null</code>, 603 * or if the <code>parentComponent</code> has no 604 * <code>Frame</code>, a default <code>Frame</code> is used 605 * @param message the <code>Object</code> to display 606 * @exception HeadlessException if 607 * <code>GraphicsEnvironment.isHeadless</code> returns 608 * <code>true</code> 609 * @see java.awt.GraphicsEnvironment#isHeadless 610 */ 611 public static void showMessageDialog(Component parentComponent, 612 Object message) throws HeadlessException { 613 showMessageDialog(parentComponent, message, UIManager.getString( 614 "OptionPane.messageDialogTitle", parentComponent), 615 INFORMATION_MESSAGE); 616 } 617 618 /** 619 * Brings up a dialog that displays a message using a default 620 * icon determined by the <code>messageType</code> parameter. 621 * 622 * @param parentComponent determines the <code>Frame</code> 623 * in which the dialog is displayed; if <code>null</code>, 624 * or if the <code>parentComponent</code> has no 625 * <code>Frame</code>, a default <code>Frame</code> is used 626 * @param message the <code>Object</code> to display 627 * @param title the title string for the dialog 628 * @param messageType the type of message to be displayed: 629 * <code>ERROR_MESSAGE</code>, 630 * <code>INFORMATION_MESSAGE</code>, 631 * <code>WARNING_MESSAGE</code>, 632 * <code>QUESTION_MESSAGE</code>, 633 * or <code>PLAIN_MESSAGE</code> 634 * @exception HeadlessException if 635 * <code>GraphicsEnvironment.isHeadless</code> returns 636 * <code>true</code> 637 * @see java.awt.GraphicsEnvironment#isHeadless 638 */ 639 public static void showMessageDialog(Component parentComponent, 640 Object message, String title, int messageType) 641 throws HeadlessException { 642 showMessageDialog(parentComponent, message, title, messageType, null); 643 } 644 645 /** 646 * Brings up a dialog displaying a message, specifying all parameters. 647 * 648 * @param parentComponent determines the <code>Frame</code> in which the 649 * dialog is displayed; if <code>null</code>, 650 * or if the <code>parentComponent</code> has no 651 * <code>Frame</code>, a 652 * default <code>Frame</code> is used 653 * @param message the <code>Object</code> to display 654 * @param title the title string for the dialog 655 * @param messageType the type of message to be displayed: 656 * <code>ERROR_MESSAGE</code>, 657 * <code>INFORMATION_MESSAGE</code>, 658 * <code>WARNING_MESSAGE</code>, 659 * <code>QUESTION_MESSAGE</code>, 660 * or <code>PLAIN_MESSAGE</code> 661 * @param icon an icon to display in the dialog that helps the user 662 * identify the kind of message that is being displayed 663 * @exception HeadlessException if 664 * <code>GraphicsEnvironment.isHeadless</code> returns 665 * <code>true</code> 666 * @see java.awt.GraphicsEnvironment#isHeadless 667 */ 668 public static void showMessageDialog(Component parentComponent, 669 Object message, String title, int messageType, Icon icon) 670 throws HeadlessException { 671 showOptionDialog(parentComponent, message, title, DEFAULT_OPTION, 672 messageType, icon, null, null); 673 } 674 675 /** 676 * Brings up a dialog with the options <i>Yes</i>, 677 * <i>No</i> and <i>Cancel</i>; with the 678 * title, <b>Select an Option</b>. 679 * 680 * @param parentComponent determines the <code>Frame</code> in which the 681 * dialog is displayed; if <code>null</code>, 682 * or if the <code>parentComponent</code> has no 683 * <code>Frame</code>, a 684 * default <code>Frame</code> is used 685 * @param message the <code>Object</code> to display 686 * @return an integer indicating the option selected by the user 687 * @exception HeadlessException if 688 * <code>GraphicsEnvironment.isHeadless</code> returns 689 * <code>true</code> 690 * @see java.awt.GraphicsEnvironment#isHeadless 691 */ 692 public static int showConfirmDialog(Component parentComponent, 693 Object message) throws HeadlessException { 694 return showConfirmDialog(parentComponent, message, 695 UIManager.getString("OptionPane.titleText"), 696 YES_NO_CANCEL_OPTION); 697 } 698 699 /** 700 * Brings up a dialog where the number of choices is determined 701 * by the <code>optionType</code> parameter. 702 * 703 * @param parentComponent determines the <code>Frame</code> in which the 704 * dialog is displayed; if <code>null</code>, 705 * or if the <code>parentComponent</code> has no 706 * <code>Frame</code>, a 707 * default <code>Frame</code> is used 708 * @param message the <code>Object</code> to display 709 * @param title the title string for the dialog 710 * @param optionType an int designating the options available on the dialog: 711 * <code>YES_NO_OPTION</code>, 712 * <code>YES_NO_CANCEL_OPTION</code>, 713 * or <code>OK_CANCEL_OPTION</code> 714 * @return an int indicating the option selected by the user 715 * @exception HeadlessException if 716 * <code>GraphicsEnvironment.isHeadless</code> returns 717 * <code>true</code> 718 * @see java.awt.GraphicsEnvironment#isHeadless 719 */ 720 public static int showConfirmDialog(Component parentComponent, 721 Object message, String title, int optionType) 722 throws HeadlessException { 723 return showConfirmDialog(parentComponent, message, title, optionType, 724 QUESTION_MESSAGE); 725 } 726 727 /** 728 * Brings up a dialog where the number of choices is determined 729 * by the <code>optionType</code> parameter, where the 730 * <code>messageType</code> 731 * parameter determines the icon to display. 732 * The <code>messageType</code> parameter is primarily used to supply 733 * a default icon from the Look and Feel. 734 * 735 * @param parentComponent determines the <code>Frame</code> in 736 * which the dialog is displayed; if <code>null</code>, 737 * or if the <code>parentComponent</code> has no 738 * <code>Frame</code>, a 739 * default <code>Frame</code> is used. 740 * @param message the <code>Object</code> to display 741 * @param title the title string for the dialog 742 * @param optionType an integer designating the options available 743 * on the dialog: <code>YES_NO_OPTION</code>, 744 * <code>YES_NO_CANCEL_OPTION</code>, 745 * or <code>OK_CANCEL_OPTION</code> 746 * @param messageType an integer designating the kind of message this is; 747 * primarily used to determine the icon from the pluggable 748 * Look and Feel: <code>ERROR_MESSAGE</code>, 749 * <code>INFORMATION_MESSAGE</code>, 750 * <code>WARNING_MESSAGE</code>, 751 * <code>QUESTION_MESSAGE</code>, 752 * or <code>PLAIN_MESSAGE</code> 753 * @return an integer indicating the option selected by the user 754 * @exception HeadlessException if 755 * <code>GraphicsEnvironment.isHeadless</code> returns 756 * <code>true</code> 757 * @see java.awt.GraphicsEnvironment#isHeadless 758 */ 759 public static int showConfirmDialog(Component parentComponent, 760 Object message, String title, int optionType, int messageType) 761 throws HeadlessException { 762 return showConfirmDialog(parentComponent, message, title, optionType, 763 messageType, null); 764 } 765 766 /** 767 * Brings up a dialog with a specified icon, where the number of 768 * choices is determined by the <code>optionType</code> parameter. 769 * The <code>messageType</code> parameter is primarily used to supply 770 * a default icon from the look and feel. 771 * 772 * @param parentComponent determines the <code>Frame</code> in which the 773 * dialog is displayed; if <code>null</code>, 774 * or if the <code>parentComponent</code> has no 775 * <code>Frame</code>, a 776 * default <code>Frame</code> is used 777 * @param message the Object to display 778 * @param title the title string for the dialog 779 * @param optionType an int designating the options available on the dialog: 780 * <code>YES_NO_OPTION</code>, 781 * <code>YES_NO_CANCEL_OPTION</code>, 782 * or <code>OK_CANCEL_OPTION</code> 783 * @param messageType an int designating the kind of message this is, 784 * primarily used to determine the icon from the pluggable 785 * Look and Feel: <code>ERROR_MESSAGE</code>, 786 * <code>INFORMATION_MESSAGE</code>, 787 * <code>WARNING_MESSAGE</code>, 788 * <code>QUESTION_MESSAGE</code>, 789 * or <code>PLAIN_MESSAGE</code> 790 * @param icon the icon to display in the dialog 791 * @return an int indicating the option selected by the user 792 * @exception HeadlessException if 793 * <code>GraphicsEnvironment.isHeadless</code> returns 794 * <code>true</code> 795 * @see java.awt.GraphicsEnvironment#isHeadless 796 */ 797 public static int showConfirmDialog(Component parentComponent, 798 Object message, String title, int optionType, 799 int messageType, Icon icon) throws HeadlessException { 800 return showOptionDialog(parentComponent, message, title, optionType, 801 messageType, icon, null, null); 802 } 803 804 /** 805 * Brings up a dialog with a specified icon, where the initial 806 * choice is determined by the <code>initialValue</code> parameter and 807 * the number of choices is determined by the <code>optionType</code> 808 * parameter. 809 * <p> 810 * If <code>optionType</code> is <code>YES_NO_OPTION</code>, 811 * or <code>YES_NO_CANCEL_OPTION</code> 812 * and the <code>options</code> parameter is <code>null</code>, 813 * then the options are 814 * supplied by the look and feel. 815 * <p> 816 * The <code>messageType</code> parameter is primarily used to supply 817 * a default icon from the look and feel. 818 * 819 * @param parentComponent determines the <code>Frame</code> 820 * in which the dialog is displayed; if 821 * <code>null</code>, or if the 822 * <code>parentComponent</code> has no 823 * <code>Frame</code>, a 824 * default <code>Frame</code> is used 825 * @param message the <code>Object</code> to display 826 * @param title the title string for the dialog 827 * @param optionType an integer designating the options available on the 828 * dialog: <code>DEFAULT_OPTION</code>, 829 * <code>YES_NO_OPTION</code>, 830 * <code>YES_NO_CANCEL_OPTION</code>, 831 * or <code>OK_CANCEL_OPTION</code> 832 * @param messageType an integer designating the kind of message this is, 833 * primarily used to determine the icon from the 834 * pluggable Look and Feel: <code>ERROR_MESSAGE</code>, 835 * <code>INFORMATION_MESSAGE</code>, 836 * <code>WARNING_MESSAGE</code>, 837 * <code>QUESTION_MESSAGE</code>, 838 * or <code>PLAIN_MESSAGE</code> 839 * @param icon the icon to display in the dialog 840 * @param options an array of objects indicating the possible choices 841 * the user can make; if the objects are components, they 842 * are rendered properly; non-<code>String</code> 843 * objects are 844 * rendered using their <code>toString</code> methods; 845 * if this parameter is <code>null</code>, 846 * the options are determined by the Look and Feel 847 * @param initialValue the object that represents the default selection 848 * for the dialog; only meaningful if <code>options</code> 849 * is used; can be <code>null</code> 850 * @return an integer indicating the option chosen by the user, 851 * or <code>CLOSED_OPTION</code> if the user closed 852 * the dialog 853 * @exception HeadlessException if 854 * <code>GraphicsEnvironment.isHeadless</code> returns 855 * <code>true</code> 856 * @see java.awt.GraphicsEnvironment#isHeadless 857 */ 858 public static int showOptionDialog(Component parentComponent, 859 Object message, String title, int optionType, int messageType, 860 Icon icon, Object[] options, Object initialValue) 861 throws HeadlessException { 862 JOptionPane pane = new JOptionPane(message, messageType, 863 optionType, icon, 864 options, initialValue); 865 866 pane.setInitialValue(initialValue); 867 pane.setComponentOrientation(((parentComponent == null) ? 868 getRootFrame() : parentComponent).getComponentOrientation()); 869 870 int style = styleFromMessageType(messageType); 871 JDialog dialog = pane.createDialog(parentComponent, title, style); 872 873 pane.selectInitialValue(); 874 dialog.show(); 875 dialog.dispose(); 876 877 Object selectedValue = pane.getValue(); 878 879 if(selectedValue == null) 880 return CLOSED_OPTION; 881 if(options == null) { 882 if(selectedValue instanceof Integer) 883 return ((Integer)selectedValue).intValue(); 884 return CLOSED_OPTION; 885 } 886 for(int counter = 0, maxCounter = options.length; 887 counter < maxCounter; counter++) { 888 if(options[counter].equals(selectedValue)) 889 return counter; 890 } 891 return CLOSED_OPTION; 892 } 893 894 /** 895 * Creates and returns a new <code>JDialog</code> wrapping 896 * <code>this</code> centered on the <code>parentComponent</code> 897 * in the <code>parentComponent</code>'s frame. 898 * <code>title</code> is the title of the returned dialog. 899 * The returned <code>JDialog</code> will not be resizable by the 900 * user, however programs can invoke <code>setResizable</code> on 901 * the <code>JDialog</code> instance to change this property. 902 * The returned <code>JDialog</code> will be set up such that 903 * once it is closed, or the user clicks on one of the buttons, 904 * the optionpane's value property will be set accordingly and 905 * the dialog will be closed. Each time the dialog is made visible, 906 * it will reset the option pane's value property to 907 * <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the 908 * user's subsequent action closes the dialog properly. 909 * 910 * @param parentComponent determines the frame in which the dialog 911 * is displayed; if the <code>parentComponent</code> has 912 * no <code>Frame</code>, a default <code>Frame</code> is used 913 * @param title the title string for the dialog 914 * @return a new <code>JDialog</code> containing this instance 915 * @exception HeadlessException if 916 * <code>GraphicsEnvironment.isHeadless</code> returns 917 * <code>true</code> 918 * @see java.awt.GraphicsEnvironment#isHeadless 919 */ 920 public JDialog createDialog(Component parentComponent, String title) 921 throws HeadlessException { 922 int style = styleFromMessageType(getMessageType()); 923 return createDialog(parentComponent, title, style); 924 } 925 926 /** 927 * Creates and returns a new parentless <code>JDialog</code> 928 * with the specified title. 929 * The returned <code>JDialog</code> will not be resizable by the 930 * user, however programs can invoke <code>setResizable</code> on 931 * the <code>JDialog</code> instance to change this property. 932 * The returned <code>JDialog</code> will be set up such that 933 * once it is closed, or the user clicks on one of the buttons, 934 * the optionpane's value property will be set accordingly and 935 * the dialog will be closed. Each time the dialog is made visible, 936 * it will reset the option pane's value property to 937 * <code>JOptionPane.UNINITIALIZED_VALUE</code> to ensure the 938 * user's subsequent action closes the dialog properly. 939 * 940 * @param title the title string for the dialog 941 * @return a new <code>JDialog</code> containing this instance 942 * @exception HeadlessException if 943 * <code>GraphicsEnvironment.isHeadless</code> returns 944 * <code>true</code> 945 * @see java.awt.GraphicsEnvironment#isHeadless 946 * @since 1.6 947 */ 948 public JDialog createDialog(String title) throws HeadlessException { 949 int style = styleFromMessageType(getMessageType()); 950 JDialog dialog = new JDialog((Dialog) null, title, true); 951 initDialog(dialog, style, null); 952 return dialog; 953 } 954 955 private JDialog createDialog(Component parentComponent, String title, 956 int style) 957 throws HeadlessException { 958 959 final JDialog dialog; 960 961 Window window = JOptionPane.getWindowForComponent(parentComponent); 962 if (window instanceof Frame) { 963 dialog = new JDialog((Frame)window, title, true); 964 } else { 965 dialog = new JDialog((Dialog)window, title, true); 966 } 967 if (window instanceof SwingUtilities.SharedOwnerFrame) { 968 WindowListener ownerShutdownListener = 969 SwingUtilities.getSharedOwnerFrameShutdownListener(); 970 dialog.addWindowListener(ownerShutdownListener); 971 } 972 initDialog(dialog, style, parentComponent); 973 return dialog; 974 } 975 976 private void initDialog(final JDialog dialog, int style, Component parentComponent) { 977 dialog.setComponentOrientation(this.getComponentOrientation()); 978 Container contentPane = dialog.getContentPane(); 979 980 contentPane.setLayout(new BorderLayout()); 981 contentPane.add(this, BorderLayout.CENTER); 982 dialog.setResizable(false); 983 if (JDialog.isDefaultLookAndFeelDecorated()) { 984 boolean supportsWindowDecorations = 985 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 986 if (supportsWindowDecorations) { 987 dialog.setUndecorated(true); 988 getRootPane().setWindowDecorationStyle(style); 989 } 990 } 991 dialog.pack(); 992 dialog.setLocationRelativeTo(parentComponent); 993 994 final PropertyChangeListener listener = new PropertyChangeListener() { 995 public void propertyChange(PropertyChangeEvent event) { 996 // Let the defaultCloseOperation handle the closing 997 // if the user closed the window without selecting a button 998 // (newValue = null in that case). Otherwise, close the dialog. 999 if (dialog.isVisible() && event.getSource() == JOptionPane.this && 1000 (event.getPropertyName().equals(VALUE_PROPERTY)) && 1001 event.getNewValue() != null && 1002 event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { 1003 dialog.setVisible(false); 1004 } 1005 } 1006 }; 1007 1008 WindowAdapter adapter = new WindowAdapter() { 1009 private boolean gotFocus = false; 1010 public void windowClosing(WindowEvent we) { 1011 setValue(null); 1012 } 1013 1014 public void windowClosed(WindowEvent e) { 1015 removePropertyChangeListener(listener); 1016 dialog.getContentPane().removeAll(); 1017 } 1018 1019 public void windowGainedFocus(WindowEvent we) { 1020 // Once window gets focus, set initial focus 1021 if (!gotFocus) { 1022 selectInitialValue(); 1023 gotFocus = true; 1024 } 1025 } 1026 }; 1027 dialog.addWindowListener(adapter); 1028 dialog.addWindowFocusListener(adapter); 1029 dialog.addComponentListener(new ComponentAdapter() { 1030 public void componentShown(ComponentEvent ce) { 1031 // reset value to ensure closing works properly 1032 setValue(JOptionPane.UNINITIALIZED_VALUE); 1033 } 1034 }); 1035 1036 addPropertyChangeListener(listener); 1037 } 1038 1039 1040 /** 1041 * Brings up an internal confirmation dialog panel. The dialog 1042 * is a information-message dialog titled "Message". 1043 * 1044 * @param parentComponent determines the <code>Frame</code> 1045 * in which the dialog is displayed; if <code>null</code>, 1046 * or if the <code>parentComponent</code> has no 1047 * <code>Frame</code>, a default <code>Frame</code> is used 1048 * @param message the object to display 1049 */ 1050 public static void showInternalMessageDialog(Component parentComponent, 1051 Object message) { 1052 showInternalMessageDialog(parentComponent, message, UIManager. 1053 getString("OptionPane.messageDialogTitle", 1054 parentComponent), INFORMATION_MESSAGE); 1055 } 1056 1057 /** 1058 * Brings up an internal dialog panel that displays a message 1059 * using a default icon determined by the <code>messageType</code> 1060 * parameter. 1061 * 1062 * @param parentComponent determines the <code>Frame</code> 1063 * in which the dialog is displayed; if <code>null</code>, 1064 * or if the <code>parentComponent</code> has no 1065 * <code>Frame</code>, a default <code>Frame</code> is used 1066 * @param message the <code>Object</code> to display 1067 * @param title the title string for the dialog 1068 * @param messageType the type of message to be displayed: 1069 * <code>ERROR_MESSAGE</code>, 1070 * <code>INFORMATION_MESSAGE</code>, 1071 * <code>WARNING_MESSAGE</code>, 1072 * <code>QUESTION_MESSAGE</code>, 1073 * or <code>PLAIN_MESSAGE</code> 1074 */ 1075 public static void showInternalMessageDialog(Component parentComponent, 1076 Object message, String title, 1077 int messageType) { 1078 showInternalMessageDialog(parentComponent, message, title, messageType,null); 1079 } 1080 1081 /** 1082 * Brings up an internal dialog panel displaying a message, 1083 * specifying all parameters. 1084 * 1085 * @param parentComponent determines the <code>Frame</code> 1086 * in which the dialog is displayed; if <code>null</code>, 1087 * or if the <code>parentComponent</code> has no 1088 * <code>Frame</code>, a default <code>Frame</code> is used 1089 * @param message the <code>Object</code> to display 1090 * @param title the title string for the dialog 1091 * @param messageType the type of message to be displayed: 1092 * <code>ERROR_MESSAGE</code>, 1093 * <code>INFORMATION_MESSAGE</code>, 1094 * <code>WARNING_MESSAGE</code>, 1095 * <code>QUESTION_MESSAGE</code>, 1096 * or <code>PLAIN_MESSAGE</code> 1097 * @param icon an icon to display in the dialog that helps the user 1098 * identify the kind of message that is being displayed 1099 */ 1100 public static void showInternalMessageDialog(Component parentComponent, 1101 Object message, 1102 String title, int messageType, 1103 Icon icon){ 1104 showInternalOptionDialog(parentComponent, message, title, DEFAULT_OPTION, 1105 messageType, icon, null, null); 1106 } 1107 1108 /** 1109 * Brings up an internal dialog panel with the options <i>Yes</i>, <i>No</i> 1110 * and <i>Cancel</i>; with the title, <b>Select an Option</b>. 1111 * 1112 * @param parentComponent determines the <code>Frame</code> in 1113 * which the dialog is displayed; if <code>null</code>, 1114 * or if the <code>parentComponent</code> has no 1115 * <code>Frame</code>, a default <code>Frame</code> is used 1116 * @param message the <code>Object</code> to display 1117 * @return an integer indicating the option selected by the user 1118 */ 1119 public static int showInternalConfirmDialog(Component parentComponent, 1120 Object message) { 1121 return showInternalConfirmDialog(parentComponent, message, 1122 UIManager.getString("OptionPane.titleText"), 1123 YES_NO_CANCEL_OPTION); 1124 } 1125 1126 /** 1127 * Brings up a internal dialog panel where the number of choices 1128 * is determined by the <code>optionType</code> parameter. 1129 * 1130 * @param parentComponent determines the <code>Frame</code> 1131 * in which the dialog is displayed; if <code>null</code>, 1132 * or if the <code>parentComponent</code> has no 1133 * <code>Frame</code>, a default <code>Frame</code> is used 1134 * @param message the object to display in the dialog; a 1135 * <code>Component</code> object is rendered as a 1136 * <code>Component</code>; a <code>String</code> 1137 * object is rendered as a string; other objects 1138 * are converted to a <code>String</code> using the 1139 * <code>toString</code> method 1140 * @param title the title string for the dialog 1141 * @param optionType an integer designating the options 1142 * available on the dialog: <code>YES_NO_OPTION</code>, 1143 * or <code>YES_NO_CANCEL_OPTION</code> 1144 * @return an integer indicating the option selected by the user 1145 */ 1146 public static int showInternalConfirmDialog(Component parentComponent, 1147 Object message, String title, 1148 int optionType) { 1149 return showInternalConfirmDialog(parentComponent, message, title, optionType, 1150 QUESTION_MESSAGE); 1151 } 1152 1153 /** 1154 * Brings up an internal dialog panel where the number of choices 1155 * is determined by the <code>optionType</code> parameter, where 1156 * the <code>messageType</code> parameter determines the icon to display. 1157 * The <code>messageType</code> parameter is primarily used to supply 1158 * a default icon from the Look and Feel. 1159 * 1160 * @param parentComponent determines the <code>Frame</code> in 1161 * which the dialog is displayed; if <code>null</code>, 1162 * or if the <code>parentComponent</code> has no 1163 * <code>Frame</code>, a default <code>Frame</code> is used 1164 * @param message the object to display in the dialog; a 1165 * <code>Component</code> object is rendered as a 1166 * <code>Component</code>; a <code>String</code> 1167 * object is rendered as a string; other objects are 1168 * converted to a <code>String</code> using the 1169 * <code>toString</code> method 1170 * @param title the title string for the dialog 1171 * @param optionType an integer designating the options 1172 * available on the dialog: 1173 * <code>YES_NO_OPTION</code>, or <code>YES_NO_CANCEL_OPTION</code> 1174 * @param messageType an integer designating the kind of message this is, 1175 * primarily used to determine the icon from the 1176 * pluggable Look and Feel: <code>ERROR_MESSAGE</code>, 1177 * <code>INFORMATION_MESSAGE</code>, 1178 * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, 1179 * or <code>PLAIN_MESSAGE</code> 1180 * @return an integer indicating the option selected by the user 1181 */ 1182 public static int showInternalConfirmDialog(Component parentComponent, 1183 Object message, 1184 String title, int optionType, 1185 int messageType) { 1186 return showInternalConfirmDialog(parentComponent, message, title, optionType, 1187 messageType, null); 1188 } 1189 1190 /** 1191 * Brings up an internal dialog panel with a specified icon, where 1192 * the number of choices is determined by the <code>optionType</code> 1193 * parameter. 1194 * The <code>messageType</code> parameter is primarily used to supply 1195 * a default icon from the look and feel. 1196 * 1197 * @param parentComponent determines the <code>Frame</code> 1198 * in which the dialog is displayed; if <code>null</code>, 1199 * or if the parentComponent has no Frame, a 1200 * default <code>Frame</code> is used 1201 * @param message the object to display in the dialog; a 1202 * <code>Component</code> object is rendered as a 1203 * <code>Component</code>; a <code>String</code> 1204 * object is rendered as a string; other objects are 1205 * converted to a <code>String</code> using the 1206 * <code>toString</code> method 1207 * @param title the title string for the dialog 1208 * @param optionType an integer designating the options available 1209 * on the dialog: 1210 * <code>YES_NO_OPTION</code>, or 1211 * <code>YES_NO_CANCEL_OPTION</code>. 1212 * @param messageType an integer designating the kind of message this is, 1213 * primarily used to determine the icon from the pluggable 1214 * Look and Feel: <code>ERROR_MESSAGE</code>, 1215 * <code>INFORMATION_MESSAGE</code>, 1216 * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, 1217 * or <code>PLAIN_MESSAGE</code> 1218 * @param icon the icon to display in the dialog 1219 * @return an integer indicating the option selected by the user 1220 */ 1221 public static int showInternalConfirmDialog(Component parentComponent, 1222 Object message, 1223 String title, int optionType, 1224 int messageType, Icon icon) { 1225 return showInternalOptionDialog(parentComponent, message, title, optionType, 1226 messageType, icon, null, null); 1227 } 1228 1229 /** 1230 * Brings up an internal dialog panel with a specified icon, where 1231 * the initial choice is determined by the <code>initialValue</code> 1232 * parameter and the number of choices is determined by the 1233 * <code>optionType</code> parameter. 1234 * <p> 1235 * If <code>optionType</code> is <code>YES_NO_OPTION</code>, or 1236 * <code>YES_NO_CANCEL_OPTION</code> 1237 * and the <code>options</code> parameter is <code>null</code>, 1238 * then the options are supplied by the Look and Feel. 1239 * <p> 1240 * The <code>messageType</code> parameter is primarily used to supply 1241 * a default icon from the look and feel. 1242 * 1243 * @param parentComponent determines the <code>Frame</code> 1244 * in which the dialog is displayed; if <code>null</code>, 1245 * or if the <code>parentComponent</code> has no 1246 * <code>Frame</code>, a default <code>Frame</code> is used 1247 * @param message the object to display in the dialog; a 1248 * <code>Component</code> object is rendered as a 1249 * <code>Component</code>; a <code>String</code> 1250 * object is rendered as a string. Other objects are 1251 * converted to a <code>String</code> using the 1252 * <code>toString</code> method 1253 * @param title the title string for the dialog 1254 * @param optionType an integer designating the options available 1255 * on the dialog: <code>YES_NO_OPTION</code>, 1256 * or <code>YES_NO_CANCEL_OPTION</code> 1257 * @param messageType an integer designating the kind of message this is; 1258 * primarily used to determine the icon from the 1259 * pluggable Look and Feel: <code>ERROR_MESSAGE</code>, 1260 * <code>INFORMATION_MESSAGE</code>, 1261 * <code>WARNING_MESSAGE</code>, <code>QUESTION_MESSAGE</code>, 1262 * or <code>PLAIN_MESSAGE</code> 1263 * @param icon the icon to display in the dialog 1264 * @param options an array of objects indicating the possible choices 1265 * the user can make; if the objects are components, they 1266 * are rendered properly; non-<code>String</code> 1267 * objects are rendered using their <code>toString</code> 1268 * methods; if this parameter is <code>null</code>, 1269 * the options are determined by the Look and Feel 1270 * @param initialValue the object that represents the default selection 1271 * for the dialog; only meaningful if <code>options</code> 1272 * is used; can be <code>null</code> 1273 * @return an integer indicating the option chosen by the user, 1274 * or <code>CLOSED_OPTION</code> if the user closed the Dialog 1275 */ 1276 public static int showInternalOptionDialog(Component parentComponent, 1277 Object message, 1278 String title, int optionType, 1279 int messageType, Icon icon, 1280 Object[] options, Object initialValue) { 1281 JOptionPane pane = new JOptionPane(message, messageType, 1282 optionType, icon, options, initialValue); 1283 pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, 1284 Boolean.TRUE); 1285 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1286 getFocusOwner(); 1287 1288 pane.setInitialValue(initialValue); 1289 1290 JInternalFrame dialog = 1291 pane.createInternalFrame(parentComponent, title); 1292 pane.selectInitialValue(); 1293 dialog.setVisible(true); 1294 1295 /* Since all input will be blocked until this dialog is dismissed, 1296 * make sure its parent containers are visible first (this component 1297 * is tested below). This is necessary for JApplets, because 1298 * because an applet normally isn't made visible until after its 1299 * start() method returns -- if this method is called from start(), 1300 * the applet will appear to hang while an invisible modal frame 1301 * waits for input. 1302 */ 1303 if (dialog.isVisible() && !dialog.isShowing()) { 1304 Container parent = dialog.getParent(); 1305 while (parent != null) { 1306 if (parent.isVisible() == false) { 1307 parent.setVisible(true); 1308 } 1309 parent = parent.getParent(); 1310 } 1311 } 1312 1313 AWTAccessor.getContainerAccessor().startLWModal(dialog); 1314 1315 if (parentComponent instanceof JInternalFrame) { 1316 try { 1317 ((JInternalFrame)parentComponent).setSelected(true); 1318 } catch (java.beans.PropertyVetoException e) { 1319 } 1320 } 1321 1322 Object selectedValue = pane.getValue(); 1323 1324 if (fo != null && fo.isShowing()) { 1325 fo.requestFocus(); 1326 } 1327 if (selectedValue == null) { 1328 return CLOSED_OPTION; 1329 } 1330 if (options == null) { 1331 if (selectedValue instanceof Integer) { 1332 return ((Integer)selectedValue).intValue(); 1333 } 1334 return CLOSED_OPTION; 1335 } 1336 for(int counter = 0, maxCounter = options.length; 1337 counter < maxCounter; counter++) { 1338 if (options[counter].equals(selectedValue)) { 1339 return counter; 1340 } 1341 } 1342 return CLOSED_OPTION; 1343 } 1344 1345 /** 1346 * Shows an internal question-message dialog requesting input from 1347 * the user parented to <code>parentComponent</code>. The dialog 1348 * is displayed in the <code>Component</code>'s frame, 1349 * and is usually positioned below the <code>Component</code>. 1350 * 1351 * @param parentComponent the parent <code>Component</code> 1352 * for the dialog 1353 * @param message the <code>Object</code> to display 1354 * @return user's input 1355 */ 1356 public static String showInternalInputDialog(Component parentComponent, 1357 Object message) { 1358 return showInternalInputDialog(parentComponent, message, UIManager. 1359 getString("OptionPane.inputDialogTitle", parentComponent), 1360 QUESTION_MESSAGE); 1361 } 1362 1363 /** 1364 * Shows an internal dialog requesting input from the user parented 1365 * to <code>parentComponent</code> with the dialog having the title 1366 * <code>title</code> and message type <code>messageType</code>. 1367 * 1368 * @param parentComponent the parent <code>Component</code> for the dialog 1369 * @param message the <code>Object</code> to display 1370 * @param title the <code>String</code> to display in the 1371 * dialog title bar 1372 * @param messageType the type of message that is to be displayed: 1373 * ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, 1374 * QUESTION_MESSAGE, or PLAIN_MESSAGE 1375 * @return user's input 1376 */ 1377 public static String showInternalInputDialog(Component parentComponent, 1378 Object message, String title, int messageType) { 1379 return (String)showInternalInputDialog(parentComponent, message, title, 1380 messageType, null, null, null); 1381 } 1382 1383 /** 1384 * Prompts the user for input in a blocking internal dialog where 1385 * the initial selection, possible selections, and all other 1386 * options can be specified. The user will able to choose from 1387 * <code>selectionValues</code>, where <code>null</code> 1388 * implies the user can input 1389 * whatever they wish, usually by means of a <code>JTextField</code>. 1390 * <code>initialSelectionValue</code> is the initial value to prompt 1391 * the user with. It is up to the UI to decide how best to represent 1392 * the <code>selectionValues</code>, but usually a 1393 * <code>JComboBox</code>, <code>JList</code>, or 1394 * <code>JTextField</code> will be used. 1395 * 1396 * @param parentComponent the parent <code>Component</code> for the dialog 1397 * @param message the <code>Object</code> to display 1398 * @param title the <code>String</code> to display in the dialog 1399 * title bar 1400 * @param messageType the type of message to be displayed: 1401 * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>, 1402 * <code>WARNING_MESSAGE</code>, 1403 * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code> 1404 * @param icon the <code>Icon</code> image to display 1405 * @param selectionValues an array of <code>Objects</code> that 1406 * gives the possible selections 1407 * @param initialSelectionValue the value used to initialize the input 1408 * field 1409 * @return user's input, or <code>null</code> meaning the user 1410 * canceled the input 1411 */ 1412 public static Object showInternalInputDialog(Component parentComponent, 1413 Object message, String title, int messageType, Icon icon, 1414 Object[] selectionValues, Object initialSelectionValue) { 1415 JOptionPane pane = new JOptionPane(message, messageType, 1416 OK_CANCEL_OPTION, icon, null, null); 1417 pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, 1418 Boolean.TRUE); 1419 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1420 getFocusOwner(); 1421 1422 pane.setWantsInput(true); 1423 pane.setSelectionValues(selectionValues); 1424 pane.setInitialSelectionValue(initialSelectionValue); 1425 1426 JInternalFrame dialog = 1427 pane.createInternalFrame(parentComponent, title); 1428 1429 pane.selectInitialValue(); 1430 dialog.setVisible(true); 1431 1432 /* Since all input will be blocked until this dialog is dismissed, 1433 * make sure its parent containers are visible first (this component 1434 * is tested below). This is necessary for JApplets, because 1435 * because an applet normally isn't made visible until after its 1436 * start() method returns -- if this method is called from start(), 1437 * the applet will appear to hang while an invisible modal frame 1438 * waits for input. 1439 */ 1440 if (dialog.isVisible() && !dialog.isShowing()) { 1441 Container parent = dialog.getParent(); 1442 while (parent != null) { 1443 if (parent.isVisible() == false) { 1444 parent.setVisible(true); 1445 } 1446 parent = parent.getParent(); 1447 } 1448 } 1449 1450 AWTAccessor.getContainerAccessor().startLWModal(dialog); 1451 1452 if (parentComponent instanceof JInternalFrame) { 1453 try { 1454 ((JInternalFrame)parentComponent).setSelected(true); 1455 } catch (java.beans.PropertyVetoException e) { 1456 } 1457 } 1458 1459 if (fo != null && fo.isShowing()) { 1460 fo.requestFocus(); 1461 } 1462 Object value = pane.getInputValue(); 1463 1464 if (value == UNINITIALIZED_VALUE) { 1465 return null; 1466 } 1467 return value; 1468 } 1469 1470 /** 1471 * Creates and returns an instance of <code>JInternalFrame</code>. 1472 * The internal frame is created with the specified title, 1473 * and wrapping the <code>JOptionPane</code>. 1474 * The returned <code>JInternalFrame</code> is 1475 * added to the <code>JDesktopPane</code> ancestor of 1476 * <code>parentComponent</code>, or components 1477 * parent if one its ancestors isn't a <code>JDesktopPane</code>, 1478 * or if <code>parentComponent</code> 1479 * doesn't have a parent then a <code>RuntimeException</code> is thrown. 1480 * 1481 * @param parentComponent the parent <code>Component</code> for 1482 * the internal frame 1483 * @param title the <code>String</code> to display in the 1484 * frame's title bar 1485 * @return a <code>JInternalFrame</code> containing a 1486 * <code>JOptionPane</code> 1487 * @exception RuntimeException if <code>parentComponent</code> does 1488 * not have a valid parent 1489 */ 1490 public JInternalFrame createInternalFrame(Component parentComponent, 1491 String title) { 1492 Container parent = 1493 JOptionPane.getDesktopPaneForComponent(parentComponent); 1494 1495 if (parent == null && (parentComponent == null || 1496 (parent = parentComponent.getParent()) == null)) { 1497 throw new RuntimeException("JOptionPane: parentComponent does " + 1498 "not have a valid parent"); 1499 } 1500 1501 // Option dialogs should be closable only 1502 final JInternalFrame iFrame = new JInternalFrame(title, false, true, 1503 false, false); 1504 1505 iFrame.putClientProperty("JInternalFrame.frameType", "optionDialog"); 1506 iFrame.putClientProperty("JInternalFrame.messageType", 1507 Integer.valueOf(getMessageType())); 1508 1509 iFrame.addInternalFrameListener(new InternalFrameAdapter() { 1510 public void internalFrameClosing(InternalFrameEvent e) { 1511 if (getValue() == UNINITIALIZED_VALUE) { 1512 setValue(null); 1513 } 1514 } 1515 }); 1516 addPropertyChangeListener(new PropertyChangeListener() { 1517 public void propertyChange(PropertyChangeEvent event) { 1518 // Let the defaultCloseOperation handle the closing 1519 // if the user closed the iframe without selecting a button 1520 // (newValue = null in that case). Otherwise, close the dialog. 1521 if (iFrame.isVisible() && 1522 event.getSource() == JOptionPane.this && 1523 event.getPropertyName().equals(VALUE_PROPERTY)) { 1524 AWTAccessor.getContainerAccessor().stopLWModal(iFrame); 1525 1526 try { 1527 iFrame.setClosed(true); 1528 } 1529 catch (java.beans.PropertyVetoException e) { 1530 } 1531 1532 iFrame.setVisible(false); 1533 } 1534 } 1535 }); 1536 iFrame.getContentPane().add(this, BorderLayout.CENTER); 1537 if (parent instanceof JDesktopPane) { 1538 parent.add(iFrame, JLayeredPane.MODAL_LAYER); 1539 } else { 1540 parent.add(iFrame, BorderLayout.CENTER); 1541 } 1542 Dimension iFrameSize = iFrame.getPreferredSize(); 1543 Dimension rootSize = parent.getSize(); 1544 Dimension parentSize = parentComponent.getSize(); 1545 1546 iFrame.setBounds((rootSize.width - iFrameSize.width) / 2, 1547 (rootSize.height - iFrameSize.height) / 2, 1548 iFrameSize.width, iFrameSize.height); 1549 // We want dialog centered relative to its parent component 1550 Point iFrameCoord = 1551 SwingUtilities.convertPoint(parentComponent, 0, 0, parent); 1552 int x = (parentSize.width - iFrameSize.width) / 2 + iFrameCoord.x; 1553 int y = (parentSize.height - iFrameSize.height) / 2 + iFrameCoord.y; 1554 1555 // If possible, dialog should be fully visible 1556 int ovrx = x + iFrameSize.width - rootSize.width; 1557 int ovry = y + iFrameSize.height - rootSize.height; 1558 x = Math.max((ovrx > 0? x - ovrx: x), 0); 1559 y = Math.max((ovry > 0? y - ovry: y), 0); 1560 iFrame.setBounds(x, y, iFrameSize.width, iFrameSize.height); 1561 1562 parent.validate(); 1563 try { 1564 iFrame.setSelected(true); 1565 } catch (java.beans.PropertyVetoException e) {} 1566 1567 return iFrame; 1568 } 1569 1570 /** 1571 * Returns the specified component's <code>Frame</code>. 1572 * 1573 * @param parentComponent the <code>Component</code> to check for a 1574 * <code>Frame</code> 1575 * @return the <code>Frame</code> that contains the component, 1576 * or <code>getRootFrame</code> 1577 * if the component is <code>null</code>, 1578 * or does not have a valid <code>Frame</code> parent 1579 * @exception HeadlessException if 1580 * <code>GraphicsEnvironment.isHeadless</code> returns 1581 * <code>true</code> 1582 * @see #getRootFrame 1583 * @see java.awt.GraphicsEnvironment#isHeadless 1584 */ 1585 public static Frame getFrameForComponent(Component parentComponent) 1586 throws HeadlessException { 1587 if (parentComponent == null) 1588 return getRootFrame(); 1589 if (parentComponent instanceof Frame) 1590 return (Frame)parentComponent; 1591 return JOptionPane.getFrameForComponent(parentComponent.getParent()); 1592 } 1593 1594 /** 1595 * Returns the specified component's toplevel <code>Frame</code> or 1596 * <code>Dialog</code>. 1597 * 1598 * @param parentComponent the <code>Component</code> to check for a 1599 * <code>Frame</code> or <code>Dialog</code> 1600 * @return the <code>Frame</code> or <code>Dialog</code> that 1601 * contains the component, or the default 1602 * frame if the component is <code>null</code>, 1603 * or does not have a valid 1604 * <code>Frame</code> or <code>Dialog</code> parent 1605 * @exception HeadlessException if 1606 * <code>GraphicsEnvironment.isHeadless</code> returns 1607 * <code>true</code> 1608 * @see java.awt.GraphicsEnvironment#isHeadless 1609 */ 1610 static Window getWindowForComponent(Component parentComponent) 1611 throws HeadlessException { 1612 if (parentComponent == null) 1613 return getRootFrame(); 1614 if (parentComponent instanceof Frame || parentComponent instanceof Dialog) 1615 return (Window)parentComponent; 1616 return JOptionPane.getWindowForComponent(parentComponent.getParent()); 1617 } 1618 1619 1620 /** 1621 * Returns the specified component's desktop pane. 1622 * 1623 * @param parentComponent the <code>Component</code> to check for a 1624 * desktop 1625 * @return the <code>JDesktopPane</code> that contains the component, 1626 * or <code>null</code> if the component is <code>null</code> 1627 * or does not have an ancestor that is a 1628 * <code>JInternalFrame</code> 1629 */ 1630 public static JDesktopPane getDesktopPaneForComponent(Component parentComponent) { 1631 if(parentComponent == null) 1632 return null; 1633 if(parentComponent instanceof JDesktopPane) 1634 return (JDesktopPane)parentComponent; 1635 return getDesktopPaneForComponent(parentComponent.getParent()); 1636 } 1637 1638 private static final Object sharedFrameKey = JOptionPane.class; 1639 1640 /** 1641 * Sets the frame to use for class methods in which a frame is 1642 * not provided. 1643 * <p> 1644 * <strong>Note:</strong> 1645 * It is recommended that rather than using this method you supply a valid parent. 1646 * 1647 * @param newRootFrame the default <code>Frame</code> to use 1648 */ 1649 public static void setRootFrame(Frame newRootFrame) { 1650 if (newRootFrame != null) { 1651 SwingUtilities.appContextPut(sharedFrameKey, newRootFrame); 1652 } else { 1653 SwingUtilities.appContextRemove(sharedFrameKey); 1654 } 1655 } 1656 1657 /** 1658 * Returns the <code>Frame</code> to use for the class methods in 1659 * which a frame is not provided. 1660 * 1661 * @return the default <code>Frame</code> to use 1662 * @exception HeadlessException if 1663 * <code>GraphicsEnvironment.isHeadless</code> returns 1664 * <code>true</code> 1665 * @see #setRootFrame 1666 * @see java.awt.GraphicsEnvironment#isHeadless 1667 */ 1668 public static Frame getRootFrame() throws HeadlessException { 1669 Frame sharedFrame = 1670 (Frame)SwingUtilities.appContextGet(sharedFrameKey); 1671 if (sharedFrame == null) { 1672 sharedFrame = SwingUtilities.getSharedOwnerFrame(); 1673 SwingUtilities.appContextPut(sharedFrameKey, sharedFrame); 1674 } 1675 return sharedFrame; 1676 } 1677 1678 /** 1679 * Creates a <code>JOptionPane</code> with a test message. 1680 */ 1681 public JOptionPane() { 1682 this("JOptionPane message"); 1683 } 1684 1685 /** 1686 * Creates a instance of <code>JOptionPane</code> to display a 1687 * message using the 1688 * plain-message message type and the default options delivered by 1689 * the UI. 1690 * 1691 * @param message the <code>Object</code> to display 1692 */ 1693 public JOptionPane(Object message) { 1694 this(message, PLAIN_MESSAGE); 1695 } 1696 1697 /** 1698 * Creates an instance of <code>JOptionPane</code> to display a message 1699 * with the specified message type and the default options, 1700 * 1701 * @param message the <code>Object</code> to display 1702 * @param messageType the type of message to be displayed: 1703 * <code>ERROR_MESSAGE</code>, 1704 * <code>INFORMATION_MESSAGE</code>, 1705 * <code>WARNING_MESSAGE</code>, 1706 * <code>QUESTION_MESSAGE</code>, 1707 * or <code>PLAIN_MESSAGE</code> 1708 */ 1709 public JOptionPane(Object message, int messageType) { 1710 this(message, messageType, DEFAULT_OPTION); 1711 } 1712 1713 /** 1714 * Creates an instance of <code>JOptionPane</code> to display a message 1715 * with the specified message type and options. 1716 * 1717 * @param message the <code>Object</code> to display 1718 * @param messageType the type of message to be displayed: 1719 * <code>ERROR_MESSAGE</code>, 1720 * <code>INFORMATION_MESSAGE</code>, 1721 * <code>WARNING_MESSAGE</code>, 1722 * <code>QUESTION_MESSAGE</code>, 1723 * or <code>PLAIN_MESSAGE</code> 1724 * @param optionType the options to display in the pane: 1725 * <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>, 1726 * <code>YES_NO_CANCEL_OPTION</code>, 1727 * <code>OK_CANCEL_OPTION</code> 1728 */ 1729 public JOptionPane(Object message, int messageType, int optionType) { 1730 this(message, messageType, optionType, null); 1731 } 1732 1733 /** 1734 * Creates an instance of <code>JOptionPane</code> to display a message 1735 * with the specified message type, options, and icon. 1736 * 1737 * @param message the <code>Object</code> to display 1738 * @param messageType the type of message to be displayed: 1739 * <code>ERROR_MESSAGE</code>, 1740 * <code>INFORMATION_MESSAGE</code>, 1741 * <code>WARNING_MESSAGE</code>, 1742 * <code>QUESTION_MESSAGE</code>, 1743 * or <code>PLAIN_MESSAGE</code> 1744 * @param optionType the options to display in the pane: 1745 * <code>DEFAULT_OPTION</code>, <code>YES_NO_OPTION</code>, 1746 * <code>YES_NO_CANCEL_OPTION</code>, 1747 * <code>OK_CANCEL_OPTION</code> 1748 * @param icon the <code>Icon</code> image to display 1749 */ 1750 public JOptionPane(Object message, int messageType, int optionType, 1751 Icon icon) { 1752 this(message, messageType, optionType, icon, null); 1753 } 1754 1755 /** 1756 * Creates an instance of <code>JOptionPane</code> to display a message 1757 * with the specified message type, icon, and options. 1758 * None of the options is initially selected. 1759 * <p> 1760 * The options objects should contain either instances of 1761 * <code>Component</code>s, (which are added directly) or 1762 * <code>Strings</code> (which are wrapped in a <code>JButton</code>). 1763 * If you provide <code>Component</code>s, you must ensure that when the 1764 * <code>Component</code> is clicked it messages <code>setValue</code> 1765 * in the created <code>JOptionPane</code>. 1766 * 1767 * @param message the <code>Object</code> to display 1768 * @param messageType the type of message to be displayed: 1769 * <code>ERROR_MESSAGE</code>, 1770 * <code>INFORMATION_MESSAGE</code>, 1771 * <code>WARNING_MESSAGE</code>, 1772 * <code>QUESTION_MESSAGE</code>, 1773 * or <code>PLAIN_MESSAGE</code> 1774 * @param optionType the options to display in the pane: 1775 * <code>DEFAULT_OPTION</code>, 1776 * <code>YES_NO_OPTION</code>, 1777 * <code>YES_NO_CANCEL_OPTION</code>, 1778 * <code>OK_CANCEL_OPTION</code> 1779 * @param icon the <code>Icon</code> image to display 1780 * @param options the choices the user can select 1781 */ 1782 public JOptionPane(Object message, int messageType, int optionType, 1783 Icon icon, Object[] options) { 1784 this(message, messageType, optionType, icon, options, null); 1785 } 1786 1787 /** 1788 * Creates an instance of <code>JOptionPane</code> to display a message 1789 * with the specified message type, icon, and options, with the 1790 * initially-selected option specified. 1791 * 1792 * @param message the <code>Object</code> to display 1793 * @param messageType the type of message to be displayed: 1794 * <code>ERROR_MESSAGE</code>, 1795 * <code>INFORMATION_MESSAGE</code>, 1796 * <code>WARNING_MESSAGE</code>, 1797 * <code>QUESTION_MESSAGE</code>, 1798 * or <code>PLAIN_MESSAGE</code> 1799 * @param optionType the options to display in the pane: 1800 * <code>DEFAULT_OPTION</code>, 1801 * <code>YES_NO_OPTION</code>, 1802 * <code>YES_NO_CANCEL_OPTION</code>, 1803 * <code>OK_CANCEL_OPTION</code> 1804 * @param icon the Icon image to display 1805 * @param options the choices the user can select 1806 * @param initialValue the choice that is initially selected; if 1807 * <code>null</code>, then nothing will be initially selected; 1808 * only meaningful if <code>options</code> is used 1809 */ 1810 public JOptionPane(Object message, int messageType, int optionType, 1811 Icon icon, Object[] options, Object initialValue) { 1812 1813 this.message = message; 1814 this.options = options; 1815 this.initialValue = initialValue; 1816 this.icon = icon; 1817 setMessageType(messageType); 1818 setOptionType(optionType); 1819 value = UNINITIALIZED_VALUE; 1820 inputValue = UNINITIALIZED_VALUE; 1821 updateUI(); 1822 } 1823 1824 /** 1825 * Sets the UI object which implements the {@literal L&F} for this component. 1826 * 1827 * @param ui the <code>OptionPaneUI</code> {@literal L&F} object 1828 * @see UIDefaults#getUI 1829 * @beaninfo 1830 * bound: true 1831 * hidden: true 1832 * description: The UI object that implements the optionpane's LookAndFeel 1833 */ 1834 public void setUI(OptionPaneUI ui) { 1835 if (this.ui != ui) { 1836 super.setUI(ui); 1837 invalidate(); 1838 } 1839 } 1840 1841 /** 1842 * Returns the UI object which implements the {@literal L&F} for this component. 1843 * 1844 * @return the <code>OptionPaneUI</code> object 1845 */ 1846 public OptionPaneUI getUI() { 1847 return (OptionPaneUI)ui; 1848 } 1849 1850 /** 1851 * Notification from the <code>UIManager</code> that the {@literal L&F} has changed. 1852 * Replaces the current UI object with the latest version from the 1853 * <code>UIManager</code>. 1854 * 1855 * @see JComponent#updateUI 1856 */ 1857 public void updateUI() { 1858 setUI((OptionPaneUI)UIManager.getUI(this)); 1859 } 1860 1861 1862 /** 1863 * Returns the name of the UI class that implements the 1864 * {@literal L&F} for this component. 1865 * 1866 * @return the string "OptionPaneUI" 1867 * @see JComponent#getUIClassID 1868 * @see UIDefaults#getUI 1869 */ 1870 public String getUIClassID() { 1871 return uiClassID; 1872 } 1873 1874 1875 /** 1876 * Sets the option pane's message-object. 1877 * @param newMessage the <code>Object</code> to display 1878 * @see #getMessage 1879 * 1880 * @beaninfo 1881 * preferred: true 1882 * bound: true 1883 * description: The optionpane's message object. 1884 */ 1885 public void setMessage(Object newMessage) { 1886 Object oldMessage = message; 1887 1888 message = newMessage; 1889 firePropertyChange(MESSAGE_PROPERTY, oldMessage, message); 1890 } 1891 1892 /** 1893 * Returns the message-object this pane displays. 1894 * @see #setMessage 1895 * 1896 * @return the <code>Object</code> that is displayed 1897 */ 1898 public Object getMessage() { 1899 return message; 1900 } 1901 1902 /** 1903 * Sets the icon to display. If non-<code>null</code>, the look and feel 1904 * does not provide an icon. 1905 * @param newIcon the <code>Icon</code> to display 1906 * 1907 * @see #getIcon 1908 * @beaninfo 1909 * preferred: true 1910 * bound: true 1911 * description: The option pane's type icon. 1912 */ 1913 public void setIcon(Icon newIcon) { 1914 Object oldIcon = icon; 1915 1916 icon = newIcon; 1917 firePropertyChange(ICON_PROPERTY, oldIcon, icon); 1918 } 1919 1920 /** 1921 * Returns the icon this pane displays. 1922 * @return the <code>Icon</code> that is displayed 1923 * 1924 * @see #setIcon 1925 */ 1926 public Icon getIcon() { 1927 return icon; 1928 } 1929 1930 /** 1931 * Sets the value the user has chosen. 1932 * @param newValue the chosen value 1933 * 1934 * @see #getValue 1935 * @beaninfo 1936 * preferred: true 1937 * bound: true 1938 * description: The option pane's value object. 1939 */ 1940 public void setValue(Object newValue) { 1941 Object oldValue = value; 1942 1943 value = newValue; 1944 firePropertyChange(VALUE_PROPERTY, oldValue, value); 1945 } 1946 1947 /** 1948 * Returns the value the user has selected. <code>UNINITIALIZED_VALUE</code> 1949 * implies the user has not yet made a choice, <code>null</code> means the 1950 * user closed the window with out choosing anything. Otherwise 1951 * the returned value will be one of the options defined in this 1952 * object. 1953 * 1954 * @return the <code>Object</code> chosen by the user, 1955 * <code>UNINITIALIZED_VALUE</code> 1956 * if the user has not yet made a choice, or <code>null</code> if 1957 * the user closed the window without making a choice 1958 * 1959 * @see #setValue 1960 */ 1961 public Object getValue() { 1962 return value; 1963 } 1964 1965 /** 1966 * Sets the options this pane displays. If an element in 1967 * <code>newOptions</code> is a <code>Component</code> 1968 * it is added directly to the pane, 1969 * otherwise a button is created for the element. 1970 * 1971 * @param newOptions an array of <code>Objects</code> that create the 1972 * buttons the user can click on, or arbitrary 1973 * <code>Components</code> to add to the pane 1974 * 1975 * @see #getOptions 1976 * @beaninfo 1977 * bound: true 1978 * description: The option pane's options objects. 1979 */ 1980 public void setOptions(Object[] newOptions) { 1981 Object[] oldOptions = options; 1982 1983 options = newOptions; 1984 firePropertyChange(OPTIONS_PROPERTY, oldOptions, options); 1985 } 1986 1987 /** 1988 * Returns the choices the user can make. 1989 * @return the array of <code>Objects</code> that give the user's choices 1990 * 1991 * @see #setOptions 1992 */ 1993 public Object[] getOptions() { 1994 if(options != null) { 1995 int optionCount = options.length; 1996 Object[] retOptions = new Object[optionCount]; 1997 1998 System.arraycopy(options, 0, retOptions, 0, optionCount); 1999 return retOptions; 2000 } 2001 return options; 2002 } 2003 2004 /** 2005 * Sets the initial value that is to be enabled -- the 2006 * <code>Component</code> 2007 * that has the focus when the pane is initially displayed. 2008 * 2009 * @param newInitialValue the <code>Object</code> that gets the initial 2010 * keyboard focus 2011 * 2012 * @see #getInitialValue 2013 * @beaninfo 2014 * preferred: true 2015 * bound: true 2016 * description: The option pane's initial value object. 2017 */ 2018 public void setInitialValue(Object newInitialValue) { 2019 Object oldIV = initialValue; 2020 2021 initialValue = newInitialValue; 2022 firePropertyChange(INITIAL_VALUE_PROPERTY, oldIV, initialValue); 2023 } 2024 2025 /** 2026 * Returns the initial value. 2027 * 2028 * @return the <code>Object</code> that gets the initial keyboard focus 2029 * 2030 * @see #setInitialValue 2031 */ 2032 public Object getInitialValue() { 2033 return initialValue; 2034 } 2035 2036 /** 2037 * Sets the option pane's message type. 2038 * The message type is used by the Look and Feel to determine the 2039 * icon to display (if not supplied) as well as potentially how to 2040 * lay out the <code>parentComponent</code>. 2041 * @param newType an integer specifying the kind of message to display: 2042 * <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>, 2043 * <code>WARNING_MESSAGE</code>, 2044 * <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code> 2045 * @exception RuntimeException if <code>newType</code> is not one of the 2046 * legal values listed above 2047 2048 * @see #getMessageType 2049 * @beaninfo 2050 * preferred: true 2051 * bound: true 2052 * description: The option pane's message type. 2053 */ 2054 public void setMessageType(int newType) { 2055 checkMessageType(newType); 2056 int oldType = messageType; 2057 messageType = newType; 2058 firePropertyChange(MESSAGE_TYPE_PROPERTY, oldType, messageType); 2059 } 2060 2061 private static void checkMessageType(int newType){ 2062 if(newType != ERROR_MESSAGE && newType != INFORMATION_MESSAGE && 2063 newType != WARNING_MESSAGE && newType != QUESTION_MESSAGE && 2064 newType != PLAIN_MESSAGE) 2065 throw new RuntimeException("JOptionPane: type must be one of" 2066 + " JOptionPane.ERROR_MESSAGE," 2067 + " JOptionPane.INFORMATION_MESSAGE," 2068 + " JOptionPane.WARNING_MESSAGE," 2069 + " JOptionPane.QUESTION_MESSAGE" 2070 + " or JOptionPane.PLAIN_MESSAGE"); 2071 } 2072 2073 /** 2074 * Returns the message type. 2075 * 2076 * @return an integer specifying the message type 2077 * 2078 * @see #setMessageType 2079 */ 2080 public int getMessageType() { 2081 return messageType; 2082 } 2083 2084 /** 2085 * Sets the options to display. 2086 * The option type is used by the Look and Feel to 2087 * determine what buttons to show (unless options are supplied). 2088 * @param newType an integer specifying the options the {@literal L&F} is to display: 2089 * <code>DEFAULT_OPTION</code>, 2090 * <code>YES_NO_OPTION</code>, 2091 * <code>YES_NO_CANCEL_OPTION</code>, 2092 * or <code>OK_CANCEL_OPTION</code> 2093 * @exception RuntimeException if <code>newType</code> is not one of 2094 * the legal values listed above 2095 * 2096 * @see #getOptionType 2097 * @see #setOptions 2098 * @beaninfo 2099 * preferred: true 2100 * bound: true 2101 * description: The option pane's option type. 2102 */ 2103 public void setOptionType(int newType) { 2104 checkOptionType(newType); 2105 int oldType = optionType; 2106 optionType = newType; 2107 firePropertyChange(OPTION_TYPE_PROPERTY, oldType, optionType); 2108 } 2109 2110 private static void checkOptionType(int newType) { 2111 if (newType != DEFAULT_OPTION && newType != YES_NO_OPTION 2112 && newType != YES_NO_CANCEL_OPTION 2113 && newType != OK_CANCEL_OPTION) { 2114 throw new RuntimeException("JOptionPane: option type must be one of" 2115 + " JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION," 2116 + " JOptionPane.YES_NO_CANCEL_OPTION" 2117 + " or JOptionPane.OK_CANCEL_OPTION"); 2118 } 2119 } 2120 2121 /** 2122 * Returns the type of options that are displayed. 2123 * 2124 * @return an integer specifying the user-selectable options 2125 * 2126 * @see #setOptionType 2127 */ 2128 public int getOptionType() { 2129 return optionType; 2130 } 2131 2132 /** 2133 * Sets the input selection values for a pane that provides the user 2134 * with a list of items to choose from. (The UI provides a widget 2135 * for choosing one of the values.) A <code>null</code> value 2136 * implies the user can input whatever they wish, usually by means 2137 * of a <code>JTextField</code>. 2138 * <p> 2139 * Sets <code>wantsInput</code> to true. Use 2140 * <code>setInitialSelectionValue</code> to specify the initially-chosen 2141 * value. After the pane as been enabled, <code>inputValue</code> is 2142 * set to the value the user has selected. 2143 * @param newValues an array of <code>Objects</code> the user to be 2144 * displayed 2145 * (usually in a list or combo-box) from which 2146 * the user can make a selection 2147 * @see #setWantsInput 2148 * @see #setInitialSelectionValue 2149 * @see #getSelectionValues 2150 * @beaninfo 2151 * bound: true 2152 * description: The option pane's selection values. 2153 */ 2154 public void setSelectionValues(Object[] newValues) { 2155 Object[] oldValues = selectionValues; 2156 2157 selectionValues = newValues; 2158 firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues); 2159 if(selectionValues != null) 2160 setWantsInput(true); 2161 } 2162 2163 /** 2164 * Returns the input selection values. 2165 * 2166 * @return the array of <code>Objects</code> the user can select 2167 * @see #setSelectionValues 2168 */ 2169 public Object[] getSelectionValues() { 2170 return selectionValues; 2171 } 2172 2173 /** 2174 * Sets the input value that is initially displayed as selected to the user. 2175 * Only used if <code>wantsInput</code> is true. 2176 * @param newValue the initially selected value 2177 * @see #setSelectionValues 2178 * @see #getInitialSelectionValue 2179 * @beaninfo 2180 * bound: true 2181 * description: The option pane's initial selection value object. 2182 */ 2183 public void setInitialSelectionValue(Object newValue) { 2184 Object oldValue = initialSelectionValue; 2185 2186 initialSelectionValue = newValue; 2187 firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, oldValue, 2188 newValue); 2189 } 2190 2191 /** 2192 * Returns the input value that is displayed as initially selected to the user. 2193 * 2194 * @return the initially selected value 2195 * @see #setInitialSelectionValue 2196 * @see #setSelectionValues 2197 */ 2198 public Object getInitialSelectionValue() { 2199 return initialSelectionValue; 2200 } 2201 2202 /** 2203 * Sets the input value that was selected or input by the user. 2204 * Only used if <code>wantsInput</code> is true. Note that this method 2205 * is invoked internally by the option pane (in response to user action) 2206 * and should generally not be called by client programs. To set the 2207 * input value initially displayed as selected to the user, use 2208 * <code>setInitialSelectionValue</code>. 2209 * 2210 * @param newValue the <code>Object</code> used to set the 2211 * value that the user specified (usually in a text field) 2212 * @see #setSelectionValues 2213 * @see #setInitialSelectionValue 2214 * @see #setWantsInput 2215 * @see #getInputValue 2216 * @beaninfo 2217 * preferred: true 2218 * bound: true 2219 * description: The option pane's input value object. 2220 */ 2221 public void setInputValue(Object newValue) { 2222 Object oldValue = inputValue; 2223 2224 inputValue = newValue; 2225 firePropertyChange(INPUT_VALUE_PROPERTY, oldValue, newValue); 2226 } 2227 2228 /** 2229 * Returns the value the user has input, if <code>wantsInput</code> 2230 * is true. 2231 * 2232 * @return the <code>Object</code> the user specified, 2233 * if it was one of the objects, or a 2234 * <code>String</code> if it was a value typed into a 2235 * field 2236 * @see #setSelectionValues 2237 * @see #setWantsInput 2238 * @see #setInputValue 2239 */ 2240 public Object getInputValue() { 2241 return inputValue; 2242 } 2243 2244 /** 2245 * Returns the maximum number of characters to place on a line in a 2246 * message. Default is to return <code>Integer.MAX_VALUE</code>. 2247 * The value can be 2248 * changed by overriding this method in a subclass. 2249 * 2250 * @return an integer giving the maximum number of characters on a line 2251 */ 2252 public int getMaxCharactersPerLineCount() { 2253 return Integer.MAX_VALUE; 2254 } 2255 2256 /** 2257 * Sets the <code>wantsInput</code> property. 2258 * If <code>newValue</code> is true, an input component 2259 * (such as a text field or combo box) whose parent is 2260 * <code>parentComponent</code> is provided to 2261 * allow the user to input a value. If <code>getSelectionValues</code> 2262 * returns a non-<code>null</code> array, the input value is one of the 2263 * objects in that array. Otherwise the input value is whatever 2264 * the user inputs. 2265 * <p> 2266 * This is a bound property. 2267 * 2268 * @param newValue if true, an input component whose parent is {@code parentComponent} 2269 * is provided to allow the user to input a value. 2270 * @see #setSelectionValues 2271 * @see #setInputValue 2272 * @beaninfo 2273 * preferred: true 2274 * bound: true 2275 * description: Flag which allows the user to input a value. 2276 */ 2277 public void setWantsInput(boolean newValue) { 2278 boolean oldValue = wantsInput; 2279 2280 wantsInput = newValue; 2281 firePropertyChange(WANTS_INPUT_PROPERTY, oldValue, newValue); 2282 } 2283 2284 /** 2285 * Returns the value of the <code>wantsInput</code> property. 2286 * 2287 * @return true if an input component will be provided 2288 * @see #setWantsInput 2289 */ 2290 public boolean getWantsInput() { 2291 return wantsInput; 2292 } 2293 2294 /** 2295 * Requests that the initial value be selected, which will set 2296 * focus to the initial value. This method 2297 * should be invoked after the window containing the option pane 2298 * is made visible. 2299 */ 2300 public void selectInitialValue() { 2301 OptionPaneUI ui = getUI(); 2302 if (ui != null) { 2303 ui.selectInitialValue(this); 2304 } 2305 } 2306 2307 2308 private static int styleFromMessageType(int messageType) { 2309 switch (messageType) { 2310 case ERROR_MESSAGE: 2311 return JRootPane.ERROR_DIALOG; 2312 case QUESTION_MESSAGE: 2313 return JRootPane.QUESTION_DIALOG; 2314 case WARNING_MESSAGE: 2315 return JRootPane.WARNING_DIALOG; 2316 case INFORMATION_MESSAGE: 2317 return JRootPane.INFORMATION_DIALOG; 2318 case PLAIN_MESSAGE: 2319 default: 2320 return JRootPane.PLAIN_DIALOG; 2321 } 2322 } 2323 2324 // Serialization support. 2325 private void writeObject(ObjectOutputStream s) throws IOException { 2326 Vector<Object> values = new Vector<Object>(); 2327 2328 s.defaultWriteObject(); 2329 // Save the icon, if its Serializable. 2330 if(icon != null && icon instanceof Serializable) { 2331 values.addElement("icon"); 2332 values.addElement(icon); 2333 } 2334 // Save the message, if its Serializable. 2335 if(message != null && message instanceof Serializable) { 2336 values.addElement("message"); 2337 values.addElement(message); 2338 } 2339 // Save the treeModel, if its Serializable. 2340 if(options != null) { 2341 Vector<Object> serOptions = new Vector<Object>(); 2342 2343 for(int counter = 0, maxCounter = options.length; 2344 counter < maxCounter; counter++) 2345 if(options[counter] instanceof Serializable) 2346 serOptions.addElement(options[counter]); 2347 if(serOptions.size() > 0) { 2348 int optionCount = serOptions.size(); 2349 Object[] arrayOptions = new Object[optionCount]; 2350 2351 serOptions.copyInto(arrayOptions); 2352 values.addElement("options"); 2353 values.addElement(arrayOptions); 2354 } 2355 } 2356 // Save the initialValue, if its Serializable. 2357 if(initialValue != null && initialValue instanceof Serializable) { 2358 values.addElement("initialValue"); 2359 values.addElement(initialValue); 2360 } 2361 // Save the value, if its Serializable. 2362 if(value != null && value instanceof Serializable) { 2363 values.addElement("value"); 2364 values.addElement(value); 2365 } 2366 // Save the selectionValues, if its Serializable. 2367 if(selectionValues != null) { 2368 boolean serialize = true; 2369 2370 for(int counter = 0, maxCounter = selectionValues.length; 2371 counter < maxCounter; counter++) { 2372 if(selectionValues[counter] != null && 2373 !(selectionValues[counter] instanceof Serializable)) { 2374 serialize = false; 2375 break; 2376 } 2377 } 2378 if(serialize) { 2379 values.addElement("selectionValues"); 2380 values.addElement(selectionValues); 2381 } 2382 } 2383 // Save the inputValue, if its Serializable. 2384 if(inputValue != null && inputValue instanceof Serializable) { 2385 values.addElement("inputValue"); 2386 values.addElement(inputValue); 2387 } 2388 // Save the initialSelectionValue, if its Serializable. 2389 if(initialSelectionValue != null && 2390 initialSelectionValue instanceof Serializable) { 2391 values.addElement("initialSelectionValue"); 2392 values.addElement(initialSelectionValue); 2393 } 2394 s.writeObject(values); 2395 } 2396 2397 private void readObject(ObjectInputStream s) 2398 throws IOException, ClassNotFoundException { 2399 ObjectInputStream.GetField f = s.readFields(); 2400 2401 int newMessageType = f.get("messageType", 0); 2402 checkMessageType(newMessageType); 2403 messageType = newMessageType; 2404 int newOptionType = f.get("optionType", 0); 2405 checkOptionType(newOptionType); 2406 optionType = newOptionType; 2407 wantsInput = f.get("wantsInput", false); 2408 2409 Vector<?> values = (Vector)s.readObject(); 2410 int indexCounter = 0; 2411 int maxCounter = values.size(); 2412 2413 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2414 equals("icon")) { 2415 icon = (Icon)values.elementAt(++indexCounter); 2416 indexCounter++; 2417 } 2418 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2419 equals("message")) { 2420 message = values.elementAt(++indexCounter); 2421 indexCounter++; 2422 } 2423 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2424 equals("options")) { 2425 options = (Object[])values.elementAt(++indexCounter); 2426 indexCounter++; 2427 } 2428 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2429 equals("initialValue")) { 2430 initialValue = values.elementAt(++indexCounter); 2431 indexCounter++; 2432 } 2433 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2434 equals("value")) { 2435 value = values.elementAt(++indexCounter); 2436 indexCounter++; 2437 } 2438 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2439 equals("selectionValues")) { 2440 selectionValues = (Object[])values.elementAt(++indexCounter); 2441 indexCounter++; 2442 } 2443 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2444 equals("inputValue")) { 2445 inputValue = values.elementAt(++indexCounter); 2446 indexCounter++; 2447 } 2448 if(indexCounter < maxCounter && values.elementAt(indexCounter). 2449 equals("initialSelectionValue")) { 2450 initialSelectionValue = values.elementAt(++indexCounter); 2451 indexCounter++; 2452 } 2453 if (getUIClassID().equals(uiClassID)) { 2454 byte count = JComponent.getWriteObjCounter(this); 2455 JComponent.setWriteObjCounter(this, --count); 2456 if (count == 0 && ui != null) { 2457 ui.installUI(this); 2458 } 2459 } 2460 } 2461 2462 2463 /** 2464 * Returns a string representation of this <code>JOptionPane</code>. 2465 * This method 2466 * is intended to be used only for debugging purposes, and the 2467 * content and format of the returned string may vary between 2468 * implementations. The returned string may be empty but may not 2469 * be <code>null</code>. 2470 * 2471 * @return a string representation of this <code>JOptionPane</code> 2472 */ 2473 protected String paramString() { 2474 String iconString = (icon != null ? 2475 icon.toString() : ""); 2476 String initialValueString = (initialValue != null ? 2477 initialValue.toString() : ""); 2478 String messageString = (message != null ? 2479 message.toString() : ""); 2480 String messageTypeString; 2481 if (messageType == ERROR_MESSAGE) { 2482 messageTypeString = "ERROR_MESSAGE"; 2483 } else if (messageType == INFORMATION_MESSAGE) { 2484 messageTypeString = "INFORMATION_MESSAGE"; 2485 } else if (messageType == WARNING_MESSAGE) { 2486 messageTypeString = "WARNING_MESSAGE"; 2487 } else if (messageType == QUESTION_MESSAGE) { 2488 messageTypeString = "QUESTION_MESSAGE"; 2489 } else if (messageType == PLAIN_MESSAGE) { 2490 messageTypeString = "PLAIN_MESSAGE"; 2491 } else messageTypeString = ""; 2492 String optionTypeString; 2493 if (optionType == DEFAULT_OPTION) { 2494 optionTypeString = "DEFAULT_OPTION"; 2495 } else if (optionType == YES_NO_OPTION) { 2496 optionTypeString = "YES_NO_OPTION"; 2497 } else if (optionType == YES_NO_CANCEL_OPTION) { 2498 optionTypeString = "YES_NO_CANCEL_OPTION"; 2499 } else if (optionType == OK_CANCEL_OPTION) { 2500 optionTypeString = "OK_CANCEL_OPTION"; 2501 } else optionTypeString = ""; 2502 String wantsInputString = (wantsInput ? 2503 "true" : "false"); 2504 2505 return super.paramString() + 2506 ",icon=" + iconString + 2507 ",initialValue=" + initialValueString + 2508 ",message=" + messageString + 2509 ",messageType=" + messageTypeString + 2510 ",optionType=" + optionTypeString + 2511 ",wantsInput=" + wantsInputString; 2512 } 2513 2514 /////////////////// 2515 // Accessibility support 2516 /////////////////// 2517 2518 /** 2519 * Returns the <code>AccessibleContext</code> associated with this JOptionPane. 2520 * For option panes, the <code>AccessibleContext</code> takes the form of an 2521 * <code>AccessibleJOptionPane</code>. 2522 * A new <code>AccessibleJOptionPane</code> instance is created if necessary. 2523 * 2524 * @return an AccessibleJOptionPane that serves as the 2525 * AccessibleContext of this AccessibleJOptionPane 2526 * @beaninfo 2527 * expert: true 2528 * description: The AccessibleContext associated with this option pane 2529 */ 2530 public AccessibleContext getAccessibleContext() { 2531 if (accessibleContext == null) { 2532 accessibleContext = new AccessibleJOptionPane(); 2533 } 2534 return accessibleContext; 2535 } 2536 2537 /** 2538 * This class implements accessibility support for the 2539 * <code>JOptionPane</code> class. It provides an implementation of the 2540 * Java Accessibility API appropriate to option pane user-interface 2541 * elements. 2542 * <p> 2543 * <strong>Warning:</strong> 2544 * Serialized objects of this class will not be compatible with 2545 * future Swing releases. The current serialization support is 2546 * appropriate for short term storage or RMI between applications running 2547 * the same version of Swing. As of 1.4, support for long term storage 2548 * of all JavaBeans™ 2549 * has been added to the <code>java.beans</code> package. 2550 * Please see {@link java.beans.XMLEncoder}. 2551 */ 2552 @SuppressWarnings("serial") // Same-version serialization only 2553 protected class AccessibleJOptionPane extends AccessibleJComponent { 2554 2555 /** 2556 * Get the role of this object. 2557 * 2558 * @return an instance of AccessibleRole describing the role of the object 2559 * @see AccessibleRole 2560 */ 2561 public AccessibleRole getAccessibleRole() { 2562 switch (messageType) { 2563 case ERROR_MESSAGE: 2564 case INFORMATION_MESSAGE: 2565 case WARNING_MESSAGE: 2566 return AccessibleRole.ALERT; 2567 2568 default: 2569 return AccessibleRole.OPTION_PANE; 2570 } 2571 } 2572 2573 } // inner class AccessibleJOptionPane 2574 }