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



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

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

何时调用它,由哪个类(大概是布局管理器)以及何时调用?我正在使用 JDK 1.7_07

我没有时间搜索所有源代码,但在测试中,看起来该方法是在打包 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 源代码支持这一点:

   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     }

相关内容

  • 没有找到相关文章

最新更新