Dial.java

This example when run, creates a top level window with a menu that has four choices, with each menu item choice selecting a different dialog box. This example is really only concerned with the followin three dialog box menu item choices:

JOptionPane Example #1
JOptionPane Example #2
JDialog Example

The fourth style of dialog box, named "Large Text Example" is not discussed here.  

From the SUN API Notes:

"""""JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialog methods shown below:

* showConfirmDialog Asks a confirming question, like yes/no/cancel.
* showInputDialog Prompt for some input.
* showMessageDialog Tell the user about something that has happened. """""""""""""""""

However, as of this example and release 1.0 of the Swing classes, the JOptionPane component is not very friendly towards accessibility technologies, and it is recommended that you do not use JOptionPane until Sun can correct this situation. Again, to view some of this information, it is suggested that you run the utility called Explorer with this java example and view the information. When looking at the JOptionPane Example #1, you will notice that the JLabel is not attached to the JTextField (the setLabelFor()) method was not called. Without this, it will be more difficult for an assistive technology like a screen reader to determine which if any label is attached to the text field to "read" it for the visually impaired user. Also, the type of information "icon" and "title" are preselected for us.

When looking at JOptionPane Example #2, we've attached the JLabel to the JTextField, but unfortunately, we are unable to place them in a JPanel, making it difficult to find the Accessible Parent. Again, this may present a problem for the screen reader assistive technology. We are also unable to add a mnemonic to the JLabel, which for a keyboard only user, might be an important means to provide efficient access to the various areas of the dialog box.

The JDialog Example allows us to provide all kinds of accessibility and usability to the dialog box. We can attach the JLabel to the JTextField, set a mnemonic for the JLabel, add Accessible Descriptions and Names to various components, as well as tooltips. We can also correct the default key actions, which should have the Return key bound to the "OK" button, and the Escape key bound to the "CANCEL"button. Once a JDialog is created, the same code could be used as a template to develop other dialog boxes.

(Note: Much of the layout code has been removed from the source listing shown here, please refer to the actual java source file for a complete listing.)

  1. JOptionPane Example #1 is created using JOptionPane.showInputDialog (one line call)as follows:

      String result = JOptionPane.showInputDialog(Dial.this, "Please enter your name:");

  2. JOptionPane Example #2 is created using JoptionPane (used directly as in API notes)and while allowing us to attach the JLabel to the JTextField, this dialog is still not as accessible as we'd like it to be.

      JLabel templab = new JLabel("My Own Text Label Here : ");
      JTextField temptxt = new JTextField(10);
      templab.setLabelFor(temptxt);

      Object[] panel = {templab, temptxt};
      JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE,
      JOptionPane.OK_CANCEL_OPTION );

      JDialog jdogtest = pane.createDialog(Dial.this, "My Own Input Title Here ");
      jdogtest.show();


  3. JDialog Example is created using "only" JDialog and can be made very accessible.