Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:07:03+00:00 2026-06-17T23:07:03+00:00

Implementing the Scrollable interface requires implementing the getPreferredScrollableViewportSize() method. This is typically done by

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:07:05+00:00Added an answer on June 17, 2026 at 11:07 pm

    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     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing Interface just provide the skeleton of the method. If we know the exact
Implementing/handling idempotency in c# with Jolivers EventStore. Does this mean just taking care of
In implementing an interface it seems the common method in tutorials and literature is
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing Serializable interface, in Java, we need to set it's serialVersionUID long. Is
I am implementing a scrollable for a portfolio gallery. (scrollable = scrollable plugin from
Implementing a 'sandbox' environment in Python used to be done with the rexec module
When implementing call-by-value lambda-calculus in Haskell, should I force the evaluation of the arguments
Implementing agile in projects requires the ability to do refactoring. It is not really
Implementing ArrayAccess Interface in PHP , We can access Object Properties as Array Keys

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.