javaswingjscrollpanescrollablejviewport

When is getPreferredScrollableViewportSize() called when laying out JScrollPane?


Implementing the Scrollable interface requires implementing the getPreferredScrollableViewportSize() method. This is typically done by just forwarding the call to getPreferredSize() - except when other parameters of the Scrollable may affect the preferred JViewport size, such as the setVisibleRowCount() method in JTree.

I have a situation where I think this method can help me achieve my goals, but a simple print statement in my implementation of getPreferredScrollableViewportSize() confirms that it is never called. A search of JScrollPane, ScrollPaneLayout and JViewport confirms there are no (direct) calls to that method. Yet, comments in JScrollPane specifically state that ScrollPaneLayout uses it, and I can confirm it is implemented as expected in JTree.

When is it called, by what class (presumably a LayoutManager) and when? I'm using JDK 1.7_07


Solution

  • I've not had the time to search through all of the source code, but on testing, it looks like the method is called when the GUI is packed.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class TestScrollable extends JPanel {
       private static final int REPACK_COUNT = 10;
       protected static final int RESIZE_COUNT = 5;
    
       public TestScrollable() {
          MyScrollable mainScrollable = new MyScrollable("Main Scrollable");
          mainScrollable.setLayout(new GridLayout(0, 1));
    
          int rowCount = 100;
          for (int i = 0; i < rowCount; i++) {
             JPanel rowPanel = new JPanel();
             String name = "Row Panel " + i;
             rowPanel.setName(name);
             rowPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
             rowPanel.setLayout(new BorderLayout());
             rowPanel.add(new JLabel(rowPanel.getName()));
             mainScrollable.add(rowPanel);
          }
    
          JViewport viewport = new JViewport() {
             @Override
             public void doLayout() {
                System.out.println("viewport doLayout called");
                super.doLayout();
             }
    
          };
          viewport.setView(mainScrollable);
    
          JScrollPane scrollPane = new JScrollPane() {
             @Override
             public void doLayout() {
                System.out.println("scrollpane doLayout called");
                super.doLayout();
             }
          };
          scrollPane.setViewport(viewport);
          setLayout(new BorderLayout());
          add(scrollPane);
       }
    
       private static void createAndShowGui() {
          TestScrollable mainPanel = new TestScrollable();
    
          final JFrame frame = new JFrame("TestScrollable") {
             @Override
             public void pack() {
                System.out.println("JFrame pack() called");
                super.pack();
             }
          };
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
    
          int delay = 1000;
          // re-test pack()
          new Timer(delay, new ActionListener() {
             private int timerCount = 0;
    
             @Override
             public void actionPerformed(ActionEvent e) {
                System.out.println("timer count: " + timerCount);
    
                if (timerCount == RESIZE_COUNT) {
                   int newWidth = frame.getSize().width * 2;
                   int newHeight = frame.getSize().height * 2;
                   Dimension newSize = new Dimension(newWidth, newHeight);
                   frame.setSize(newSize);
                   frame.repaint();
                }
    
                if (timerCount == REPACK_COUNT) {
                   System.out.println("calling pack again");
                   frame.pack();
                   ((Timer) e.getSource()).stop();
                }
                timerCount++;
             }
          }).start();
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class MyScrollable extends JComponent implements Scrollable {
       public static final int VP_WIDTH = 600;
       private static final int ROW_COUNT = 10;
    
       public MyScrollable(String name) {
          super.setName(name);
       }
    
       @Override
       public Dimension getPreferredScrollableViewportSize() {
          System.out.println(getName()
                + " getPreferredScrollableViewportSize called");
          Component[] comps = getComponents();
          if (comps.length > 0) {
             int height = ROW_COUNT * comps[0].getPreferredSize().height;
             return new Dimension(VP_WIDTH, height);
          }
    
          return super.getPreferredSize();
       }
    
       @Override
       public Dimension getPreferredSize() {
          System.out.println(getName() + " getPreferredSize called");
          return super.getPreferredSize();
       }
    
       @Override
       public int getScrollableBlockIncrement(Rectangle visibleRect,
             int orientation, int direction) {
          if (orientation == SwingConstants.HORIZONTAL) {
             return VP_WIDTH;
          }
          Component[] comps = getComponents();
          if (comps.length > 0) {
             return comps[0].getHeight() * (3 * ROW_COUNT / 4);
          }
    
          return getSize().height / 3;
       }
    
       @Override
       public boolean getScrollableTracksViewportHeight() {
          return false;
       }
    
       @Override
       public boolean getScrollableTracksViewportWidth() {
          return true;
       }
    
       @Override
       public int getScrollableUnitIncrement(Rectangle visibleRect,
             int orientation, int direction) {
          if (orientation == SwingConstants.HORIZONTAL) {
             return VP_WIDTH;
          }
          Component[] comps = getComponents();
          if (comps.length > 0) {
             return comps[0].getHeight();
          }
          return getSize().height / 3;
       }
    
    }
    

    which returns:

    JFrame pack() called
    Main Scrollable getPreferredScrollableViewportSize called
    Main Scrollable getPreferredSize called
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    timer count: 0
    timer count: 1
    timer count: 2
    timer count: 3
    timer count: 4
    timer count: 5
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    timer count: 6
    timer count: 7
    timer count: 8
    timer count: 9
    timer count: 10
    calling pack again
    JFrame pack() called
    Main Scrollable getPreferredScrollableViewportSize called
    Main Scrollable getPreferredSize called
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    scrollpane doLayout called
    Main Scrollable getPreferredSize called
    viewport doLayout called
    Main Scrollable getPreferredSize called
    

    Edit 2
    When I changed my getPreferredScrollableViewportSize override to:

    @Override
    public Dimension getPreferredScrollableViewportSize() {
      System.out.println(getName()
            + " getPreferredScrollableViewportSize called");
      StackTraceElement[] foo = Thread.currentThread().getStackTrace();
      int maxTraces = 10;
      for (int i = 0; i < foo.length && i < maxTraces ; i++) {
         System.out.printf("%02d: %s%n", i, foo[i]);
      }
      if (getComponentCount() > 0) {
         Component[] comps = getComponents();
         int height = ROW_COUNT * comps[0].getPreferredSize().height;
         return new Dimension(VP_WIDTH, height);
      }
    
      return super.getPreferredSize();
    }
    

    This is what I saw:

    Main Scrollable getPreferredScrollableViewportSize called
    00: java.lang.Thread.getStackTrace(Unknown Source)
    01: pkg.MyScrollable.getPreferredScrollableViewportSize(TestScrollable.java:115)
    02: javax.swing.ViewportLayout.preferredLayoutSize(Unknown Source)
    03: java.awt.Container.preferredSize(Unknown Source)
    04: java.awt.Container.getPreferredSize(Unknown Source)
    05: javax.swing.JComponent.getPreferredSize(Unknown Source)
    06: javax.swing.ScrollPaneLayout.preferredLayoutSize(Unknown Source)
    07: java.awt.Container.preferredSize(Unknown Source)
    08: java.awt.Container.getPreferredSize(Unknown Source)
    09: javax.swing.JComponent.getPreferredSize(Unknown Source)
    

    suggesting that the ViewportLayout class's preferredLayoutSize is what calls the Scrollable's getPreferredScrollableViewportSize method.

    Edit 3
    And in fact, the ViewportLayout source code supports this:

       86     public Dimension preferredLayoutSize(Container parent) {
       87         Component view = ((JViewport)parent).getView();
       88         if (view == null) {
       89             return new Dimension(0, 0);
       90         }
       91         else if (view instanceof Scrollable) {
       92             return ((Scrollable)view).getPreferredScrollableViewportSize();
       93         }
       94         else {
       95             return view.getPreferredSize();
       96         }
       97     }