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

JViewport has undocummented reliance on its view's getPreferredSize()

XMLWordPrintable

    • merlin
    • generic, x86
    • generic, windows_95, windows_nt



      Name: rm29839 Date: 01/21/98


      The Viewport layoutManager relies upon an "accurate" value
      being provided by view.getPreferredSize(). As a result,
      JComponent views with a default layoutManager (spring I think)
      will initially be sized incorrectly.

      The requirement that the view report an accurate preferred size
      is especially non-intuitive, since the view, typically a Scrollable,
      takes care of its own size and it is the job of the Viewport to
      REACT to changes in the view size and not actively mess with the
      non-tracking dimensions, e.g.
          getScrollableTracksViewportHeight() returns false

      In the context of JScrollPane, this means that the associated
      JScrollBar will have an incorrect value when the scrollpane
      is displayed, and will continue to have an incorrect value
      until the view contents dictate that it programmatically resize
      itself.

      This code excerpt from java.awt.swing.ViewPortLayout.java
      should make the source of the problem:

      public void layoutContainer(Container parent)
      {
          JViewport vp = (JViewport)parent;
          Component view = vp.getView();
          ................

              // here you query component's pref size
          Dimension viewPrefSize = view.getPreferredSize();
          Dimension vpSize = vp.getSize();
          ................

              // The problem below, is that prefsize may have no inherent
              // meaning, and may in fact be huge!!
          if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
              viewPrefSize.height = vpSize.height;
          }
      // the same applies here
          if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
              viewPrefSize.width = vpSize.width;
          }

          vp.setViewPosition(viewPosition);
          vp.setViewSize(viewPrefSize);
      }
      (Review ID: 23466)
      ======================================================================

      Name: krT82822 Date: 05/30/99


      Tested on Windows NT running JDK 1.2.0.

      Try running the following program. When the dialog box appears,
      all items in the list are visible, and no scrollbars are shown.
      This is the correct behavior. Now, try resizing the dialog so
      that it is too small to show all of the items in the list.
      Notice that no scrollbar appears, which is incorrect behavior.
      Next, try resizing the dialog one more time, and a vertical
      scrollbar will magically appear. It should have appeared on the
      first resize.

      Here is the code:

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

      public class ListDialog
      {
      public static void main(String[] args)
      {
      String[] names = {"Arlo", "Cosmo", "Elmo", "Hugo"};
      JList list = new JList(names);
      JDialog dialog = new JDialog();
      dialog.getContentPane().add(new JScrollPane(list));
      dialog.pack();
      dialog.setVisible(true);
      }
      }
      (Review ID: 53086)
      ======================================================================

      Name: krT82822 Date: 05/30/99


      The problem happens when creating a JScrollPane that has a
      JList box as a viewport. If the viewport can initially display
      all of the items (without vertical scrolling) and the viewport is made
      smaller (by dragging the parent window to a smaller size(so that all the
      items can't be displayed without Vertical scrolling) the
      Vertical Scroll bar will not be displayed until a second
      attemp of re-sizing the window is made.

      In other words, the first time the frame is manually made too
      small to display all the items, the Vertcal Scroll bars are not
      painted. Only a second attempt, at manually resizing the
      frame, will show the Vertical scroll bar.

      The following trivial program verifies this.








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


      public class MyScroll extends JFrame {
      JPanel panel;
      JScrollPane pane;
      JList list;
      String[] item = {"Florida", "Georgia", "South Carolina", "North Carolina", "Virginia", "Maryland", "Delaware", "New Jersey", "New York"};

      public MyScroll()
      {
      setTitle("List Box With Scroll Bars");
      setSize(300, 250);
      setBackground(Color.gray);

      list = new JList(item);
      pane = new JScrollPane(list);
      getContentPane().add(pane); //Adds list box to content pane (JPanel) object using
      //default BorderLayout.Center


      }

      public static void main(String argc[])
      {
      MyScroll obj = new MyScroll();
      obj.setVisible(true);
      }
      }
      (Review ID: 53923)
      ======================================================================

      Name: krT82822 Date: 05/30/99


      I've placed a JPanel inside a JScrollPane, and the JPanel is being sized too small. It's using its preferred scrollable viewport size as its preferred size, although the real preferred size should be much larger.

      In the following program, the scrollbar initially lets me see all 30 labels, about 9 at a time on my system (panel height = 660, viewport height = 200). But after I shrink the frame window slightly, the scrollbar becomes too limited -- it only lets me see 9 labels altogether (panel height = 200). The only way I can see labels 10-30 is to make the frame window larger. The panel's layout manager still reports the correct preferred height (660), but the panel itself has the wrong preferred height (200).

      -----------

      java full version "JDK-1.2-V"

      -----------

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

      public class ScrollBug {
       private static class ScrollBugPanel extends JPanel implements Scrollable {
        ScrollBugPanel() {
         setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
         for (int i = 0; i < 30; i++) {
          add (new JLabel (" Label " + i + " "));
          add (Box.createRigidArea (new Dimension (0, 5)));
         }
        }

        public Dimension getPreferredScrollableViewportSize() {
         Dimension d = getPreferredSize();
         if (d.height > 200)
          d.height = 200;
         return d;
        }

        public int getScrollableUnitIncrement (Rectangle visibleRect,
          int orientation, int direction) {
         return 5;
        }

        public int getScrollableBlockIncrement (Rectangle visibleRect,
          int orientation, int direction) {
         return 50;
        }

        public boolean getScrollableTracksViewportWidth() {
         return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
        }

        public boolean getScrollableTracksViewportHeight() {
         return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
        }
       }

       public static void main (String[] args) {
        JFrame f = new JFrame ("Big Grid Bag");
        f.addWindowListener (new WindowAdapter() {
         public void windowClosing (WindowEvent e) {
          System.exit (0);
         }
        });

        JPanel panel = new ScrollBugPanel();
        JScrollPane scroller = new JScrollPane (panel);
        f.getContentPane().add (scroller, BorderLayout.WEST);

        f.pack();
        f.setVisible (true);
       }
      }
      (Review ID: 54102)
      ======================================================================

      Name: krT82822 Date: 08/11/99


      Neither JViewport or ViewportLayout ever call method
      Scrollable.getPreferredScrollableViewportSize(). Both
      rely on Component.getPreferredSize(). Check out methods
      JViewport.getViewSize() and ViewportLayout.layoutContainer(...).

      Someone thought it was a good idea to have a separate
      preferred size for scrollable components. Choosing to
      implement such a feature is better left to minds brighter
      than my own. I only ask that when such decisions are made,
      someone documents the deviation, so that numbskulls like
      myself don't burn a few hours trying to figure out why it
      doesn't work.

      PS- You guys should add a Bug Database field/value to denote
      documentation bugs.
      (Review ID: 93778)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: