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

Class Cast Exception: JOptionPane.showInternalInputDialog(....)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.0, 1.2.2, 1.3.0
    • client-libs
    • beta
    • generic, x86
    • generic, windows_95



      Name: dbT83986 Date: 08/14/99


      According to documentation, there are a number of signatures for showInternalInputDialog:

      -- showInternalInputDialog(Component parentComponent, Object message)
      -- showInternalInputDialog(Component parentComponent, Object message, String title, int messageType)
      -- showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

      The first two in this list return type String, the last one returns type Object.
      This makes sense; if we have specified an array of selection Objects, we want to get our object back, not a string representation of the object. Were it that simple:


      >From jdk1.2.2\src\javax\swing\JOptionPane.java (created by unarchiving the src.jar file included in Java SDK 1.2.2):
      The first two method signatures above call the third method signature with some default values set:

      public static String showInternalInputDialog(Component parentComponent, Object message) {
        return showInternalInputDialog(parentComponent, message, "Input", QUESTION_MESSAGE);
          }

      and

      public static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType) {
              return (String)showInternalInputDialog(parentComponent, message, title, messageType, null, null, null);
          }

      Note: the second method explicitly casts its return type, the first method does NOT. Why can the first method get away with that? Because the third method -- the one that is documented as returning Object -- does the casting for it:

      public static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) {

      // ...
      // method clipped for brevity
      // ...

        if(value == UNINITIALIZED_VALUE)
            return null;
        return (String)value;
      }

      Ah ha! The "fully signed" method, that should return Object, is forcing a cast into String. Unfortunately, in the project I'm working on, my Project object doesn't take kindly to this, and throws exceptions all over. It would be nice if the third, "fully signed" method did not cast the object but let the first two "convenience" methods do the casting -- as one of them already does.

      NOTE: Although I have not tested it, I suspect that the same behavior may appear in JOptionPane.showInputDialog(...) as well.

      Thank you for your help.
      (Review ID: 93925)
      ======================================================================

      Added program to reproduce:



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

      public class Test {

          public static void main(String args[]) {

              JButton sels[] = new JButton[3];
              sels[0] = new JButton("One");
              sels[1] = new JButton("Two");
              sels[2] = new JButton("Three");

              JFrame f = new JFrame();
              JDesktopPane d = new JDesktopPane();
              f.getContentPane().add(d, BorderLayout.CENTER);
              f.setSize(800,600);
              f.setVisible(true);

              Object foo = JOptionPane.showInternalInputDialog(d,
                            "Pick one", "Test", JOptionPane.QUESTION_MESSAGE, null,
                            sels, sels[1]);

              System.out.println(foo);
          }

      }

      amy.fowler@Eng 2000-02-10

            amfowler Anne Fowler (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: