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

java.awt.event.ContainerEvent has wrong constructor

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      tiger
    • CPU:
      generic, sparc
    • OS:
      generic, solaris_2.5

      Description



      Name: mgC56079 Date: 09/30/97



      The ContainerEvent has the constructor:
          public ContainerEvent(Component source, int id, Component child)
      should instead have:
          public ContainerEvent(Container source, int id, Component child)

      The getContainer() method will fail if the source is not a container
      (comment in this method says that cast is always OK. It isn't.)

      ---- The ContainerEvent's source ----
          /**
           * Constructs a ContainerEvent object.
           *
           * @param source the Component object (container) that originated the event
           * @param id an integer indicating the type of event
           * @param child the component that was added or removed
           */
          public ContainerEvent(Component source, int id, Component child) {
              super(source, id);
              this.child = child;
          }

          /**
           * Returns the originator of the event.
           *
           * @return the Container object that originated the event
           */
          public Container getContainer() {
              return (Container)source; // cast should always be OK, type was checked in constructor
          }

      ---- Here is the test (CE.java) ----
      import java.awt.Button;
      import java.awt.Container;
      import java.awt.event.ContainerEvent;

      public class CE {
      public static void main(String[] args) {
        Button b=new Button("label");
        ContainerEvent ce=new ContainerEvent(b,ContainerEvent.COMPONENT_ADDED,b);
        Container c=ce.getContainer();
        System.exit(0); //will not exit under 1.2 without this line
      }
      }
      ---- output from the test ----
      % java CE
      java.lang.ClassCastException: java.awt.Button
      at java.awt.event.ContainerEvent.getContainer(ContainerEvent.java:86)
      at CE.main(CE.java:9)
      -----------

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            son Oleg Sukhodolsky (Inactive)
            Reporter:
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: