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

JScrollPane's use of JViewport throws an ArrayIndexOutOfBoundsException

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      JScrollPane calls setViewport() on a JViewport that does not yet have an associated view Component, so the resulting call to JViewport.getView() always throws an ArrayIndexOutOfBoundsException.

      The fix is simple: both the JScrollPane constructor and its setViewportView() method should initalise the JViewport's view before calling setViewport().

      // this is the modified constructor
          public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
          {
      setLayout(new ScrollPaneLayout.UIResource());
              setVerticalScrollBarPolicy(vsbPolicy);
              setHorizontalScrollBarPolicy(hsbPolicy);
              if (view != null) {
                  setViewportView(view);
              }
      setVerticalScrollBar(createVerticalScrollBar());
      setHorizontalScrollBar(createHorizontalScrollBar());
      setOpaque(true);
              updateUI();

      if (!this.getComponentOrientation().isLeftToRight()) {
      viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0));
      }
          }

      // this is the modified mutator for the JScrollPane's view Component
          public void setViewportView(Component view) {
              if (getViewport() == null) {
                  JViewport port = createViewport();
                  if (view != null) {
                      port.setView(view);
                  }
                  setViewport(port);
                  return;
              }
              getViewport().setView(view);
          }

      JUSTIFICATION :
      Exceptions are an inefficient way to detect error situations; it is worse when a standard use of Swing causes exceptions to be thrown every time, even if they are caught internally.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Swing should use it's own API's in a way that don't trigger exceptions. This is more efficient (faster, which Swing needs to be) and it makes it easier to debug application code.

            svioletsunw Scott Violet (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: