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