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

CardLayout.add throws illegal argument exception

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      CardLayout.add(Component) throws an exception "Illegal argument".

      The sample code works on 1.0.2 but 1.1.

      import java.awt.*;
      import java.util.*;
      import java.io.*;
      import java.net.*;
      import java.applet.*;
      import java.lang.*;

      public class Test extends Applet {
      Panel cardPanel;

      public void init() {
        Panel panel;
        cardPanel = new Panel();
        cardPanel.setLayout( new CardLayout(10,10));
        cardPanel.add( new Button( "card first"));
        cardPanel.add( new Button( "card second" ));
        cardPanel.add( new Button( "card third" ));
        cardPanel.add( new Button( "card fourth" ));
        cardPanel.add( new Button( "card fifth" ));
        add( cardPanel );
        resize( 300, 400 );
        show();
          }

      public boolean action(Event e, Object o) {
        ((CardLayout)cardPanel.getLayout()).next( cardPanel );
        return true;
        }
      }

      <!DOCTYPE HTML PUBLIC "-//SQ//DTD HTML 2.0 HoTMetaL + extensions//EN">
      <HTML><HEAD><TITLE>Listing 5-1: Using LayoutManagers</TITLE></HEAD>
      <BODY>
      <applet code="Test.class" height=100 width=100>
      </applet>
      </BODY></HTML>
      ~
      ~

      Result:
      appletviewer Test.html
      java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string
              at java.awt.CardLayout.addLayoutComponent(CardLayout.java:100)
              at java.awt.Container.addImpl(Container.java)
              at java.awt.Container.add(Container.java)
              at Test.init(Test.java:15)
              at sun.applet.AppletPanel.run(AppletPanel.java:273)
              at java.lang.Thread.run(Thread.java)


      add method in Container.java passes "null" as constraints as shown below

          /**
           * Adds the specified component to this container.
           * @param comp the component to be added
           */
          public Component add(Component comp) {
              addImpl(comp, null, -1);
              return comp;
          }


      while addLayoutComponent in CardLayout.java checks if constraints is
      an instance of String as shown below.

          public void addLayoutComponent(Component comp, Object constraints) {
              if (constraints instanceof String) {
                  addLayoutComponent((String)constraints, comp);
              } else {
                  throw new IllegalArgumentException("cannot add to layout: constraint
       must be a string");
              }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                sishidasunw Shoji Ishida (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: