Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4813436

JOptionPane.showOptionDialog does not return if "options" is JButton[]

XMLWordPrintable

    • Fix Understood
    • x86
    • linux



      Name: jk109818 Date: 02/05/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OPERATING SYSTEM VERSION : glibc-2.2-7, Linux 2.2.18,
      SuSE Linux 7.1 (i386)


      A DESCRIPTION OF THE PROBLEM :
      JOptionPane.showOptionDialog() has an argument "options" of
      type Object[]. In the API it says:

      "options - an array of objects indicating the possible
      choices the user can make; if the objects are components,
      they are rendered properly; non-String objects are rendered
      using their toString methods; if this parameter is null, the
      options are determined by the Look and Feel"

      The test case provides three alternatives for options:
      1. null,
      2. String[]
      3. JButton[]

      In case of 1 and 2, JOptionPane behaves as expected. In case
      of 3 the buttons are rendered properly and the button
      specified for initialValue is focused. But obviously it has
      no effect to click on any of the buttons. The API of
      JOptionPane does not say that and how the custom buttons
      should listen to events. Thus, the user expects that
      JOptionPane will handle the events and return with the
      appropriate value - just as it does in case of 2.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. The test case has three lines that compile only
      exclusively. Each of the lines provides the different
      "options" and "initialValue" arguments for the JOptionPane.
      Uncomment the line with the arguments you want to test and
      comment the two other lines.

      2. Run the test case with the different "options" arguments.
        To open the option pane, click on the button "JOptionPane".

      3. In case of JButton[] as "options" argument the option
      pane won't close if any of the buttons is clicked.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      If it is possible to specify any type of Object array as
      "options" than the API should be clear on the effect this
      will have on the behaviour of JOptionPane. So either this is
      a bug in the implementation, or in the API documentation. In
      case of the latter, some information should be provided on
      how to achieve that the option pane returns appropriately.
      If this is to complex, there should be a note that the user
      should rather construct a custom option pane.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      The error is that the JOptionPane does not return.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * JOptionPaneTest.java
       */

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;

      public class JOptionPaneTest
      {
      private JFrame f = null;

      public static void main(String[] args)
      {
      new JOptionPaneTest();
      }

      public JOptionPaneTest()
      {
      initComponents();
      }

      private void initComponents()
      {
      f = new JFrame("JOptionPaneTest - dummy frame");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      final Container content = f.getContentPane();
      content.setLayout(new GridLayout(0,1));

      content.add(new JLabel("main frame (parent)", SwingConstants.CENTER));

      final JButton invokeButton = new JButton("JOptionPane");
      invokeButton.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent e)
      {
      showDialog();
      }
      });
      content.add(invokeButton);

      f.setSize(200,200);

      // works only with > 1.4
      f.setLocationRelativeTo(null);
      // end > 1.4

      f.setVisible(true);
      }

      private void showDialog()
      {
      final JButton okButton = new JButton("ok (button)");
      final JButton cancelButton = new JButton("cancel (button)");

      final JPanel inputPanel = new JPanel();
      inputPanel.add(new JLabel("input:"));
      inputPanel.add(new JTextField(20));

      int result = JOptionPane.showOptionDialog(f, inputPanel, "test dialog",
      JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
      //new JButton[]{okButton,cancelButton}, okButton);
      new String[]{"ok (string)", "cancel (string)"}, "ok (string)");
      //null, null);

      if (result == JOptionPane.OK_OPTION)
      {
      f.setBackground(Color.BLUE);
      }
      else
      {
      f.setBackground(Color.ORANGE);
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Specify the options as String[].
      (Review ID: 178655)
      ======================================================================

            Unassigned Unassigned
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: