Dear all,
I would like to implement a scroll pane which is resizable, but not to a size exceeding the maximum size of the java.awt.Canvas that it contains.
I've sort of managed to do this by writing a subclass of java.awt.ScrollPane which implements java.awt.event.ComponentListener and has a componentResized method that checks whether the ScrollPane's viewport width (height) exceeds the content's preferred size, and if so, resizes the pane appropriately (see code below).
It seems to me, however, that there ought to be a simpler way to achieve this.
One slightly weird thing is that when the downsizing of the pane happens, the content can
once be moved to the left by sliding the horizontal scrollbar, but not by clicking on the arrows. This causes one column of gray pixels to disappear and the rightmost column of the content to appear; subsequent actions on the scrollbar does not have any further effect. Likewise, the vertical scrollbar can also be moved up once.
Also, I would like a java.awt.Frame containing such a restrictedly resizable scrollpane, such that the Frame cannot be resized by the user such that its inside is larger than the maximum size of the scrollpane. The difficulty I encountered with that is that setSize on a Frame appears to set the size of the window
including the decorations provided by the window manager (fvwm2, if that matters), and I haven't been able to find anything similar to getViewportSize, which would let me find out the size of the area inside the Frame which is available for the scrollpane which the frame contains.
Thanks in advance for hints and advice.
Here's the code of the componentResized method:
public void componentResized(java.awt.event.ComponentEvent e)
{
java.awt.Dimension contentSize = this.content.getPreferredSize();
this.content.setSize(contentSize);
java.awt.Dimension viewportSize = getViewportSize();
System.err.println("MaxSizeScrollPane: contentSize = " + contentSize);
System.err.println("MaxSizeScrollPane: viewportSize = " + viewportSize);
int dx = Math.max(0, (int) (viewportSize.getWidth() - contentSize.getWidth()));
int dy = Math.max(0, (int) (viewportSize.getHeight() - contentSize.getHeight()));
System.err.println("MaxSizeScrollPane: dx = " + dx + ", dy = " + dy);
if ((dx > 0) || (dy > 0))
{
java.awt.Dimension currentSize = getSize();
System.err.println("MaxSizeScrollPane: currentSize = " + currentSize);
setSize(new java.awt.Dimension(((int) currentSize.getWidth()) - dx, ((int) currentSize.getHeight()) - dy));
}
System.err.println();
}
Best regards, Jan