何时布局JScrollPane时调用getPreferredScrollableViewportSize()?

时间:2013-01-26 16:03:29

标签: java swing jscrollpane scrollable jviewport

实现Scrollable接口需要实现getPreferredScrollableViewportSize()方法。这通常仅通过将调用转发到getPreferredSize()来完成 - 除非Scrollable的其他参数可能影响首选JViewport大小,例如JTree中的setVisibleRowCount()方法。

我认为这种方法可以帮助我实现目标,但是我的getPreferredScrollableViewportSize()实现中的一个简单的print语句确认它永远不会被调用。搜索JScrollPane,ScrollPaneLayout和JViewport确认没有(直接)调用该方法。然而,JScrollPane中的注释明确指出ScrollPaneLayout使用它,我可以确认它是在JTree中按预期实现的。

什么时候(大概是一个LayoutManager)和什么时候调用它?我正在使用JDK 1.7_07

1 个答案:

答案 0 :(得分:3)

我没有时间搜索所有源代码,但在测试时,看起来在GUI打包时调用该方法。

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;
   }

}

返回:

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

编辑2
当我将getPreferredScrollableViewportSize覆盖更改为:

@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();
}

这就是我所看到的:

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)

建议ViewportLayout类的preferredLayoutSize调用Scrollable的getPreferredScrollableViewportSize方法。

编辑3
事实上,ViewportLayout source code支持这一点:

   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     }