Javaworld's JScrollableDesktopPane
is no longer available on their website. I managed to scrounge up some copies of it but none of them work.
A simple solution I've derived can be achieved doing something like the following. It's not the prettiest but it certainly works better than the default behavior.
public class Window extends Frame {
JScrollPane scrollContainer = new JScrollPane();
JDesktopPane mainWorkingPane = new JDesktopPane();
public Window() {
scrollContainer.setViewportView(mainWorkingPane);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent evt) {
revalidateDesktopPane();
}
});
}
private void revalidateDesktopPane() {
Dimension dim = new Dimension(0,0);
Component[] com = mainWorkingPane.getComponents();
for (int i=0 ; i<com.length ; i++) {
int w = (int) dim.getWidth()+com[i].getWidth();
int h = (int) dim.getHeight()+com[i].getHeight();
dim.setSize(new Dimension(w,h));
}
mainWorkingPane.setPreferredSize(dim);
mainWorkingPane.revalidate();
revalidate();
repaint();
}
}
The idea being to wrap JDesktopPane
in a JScrollPane
, add a resize listener on the main Frame and then evaluate the contents of the JDesktopPane
on resize (or adding new elements).
Hope this helps someone out there.