すべてのソース コードを検索する時間はありませんでしたが、テストでは、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 }