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

JTabbedPane.getSelectedComponent throws ArrayIndexOutOfBoundException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5
    • client-libs



      Name: dgC58589 Date: 01/22/98


      I subclassed JTabbedPane and overloaded the setSelectedIndex method as follow:

      public void setSelectedIndex(int theIndex)
      {
          Component c = getSelectedComponent();
          ...
      }

      When the JTabbedPane is first created it calls the setSelectedIndex method. At this point there is not currently selected component. I would expect getSelectedComponent to return null but instead it throws an exception. The source code for JTabbedPane.getSelectedComponent is:

      return getComponentAt(getSelectedIndex());

      Since getSelectedIndex returns -1 at this point it is getComponentAt that throws the exception.

      I would rewrite getSelectedComponent as:

      Component c = null;
      int i = getSelectedIndex();
      if (i != -1)
          c = getComponentAt(i);
      return c;
      (Review ID: 23769)
      ======================================================================

            amfowler Anne Fowler (Inactive)
            dgrahamcsunw David Graham-cumming (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: