N
Nelson Ashton
I have a window that sometimes displays fairly large amounts of data.
When this happened, the window would end up larger than the screen, so
I shoehorned everything into a JScrollPane, which didn't make any
difference. I then tried
frame.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
without any effect. Apparently JFrame ignores its own maximum size
when pack() is called.
Which led to:
frame.pack();
Rectangle bounds = frame.getBounds();
if (bounds.width > maxWidth || bounds.height > maxHeight) {
frame.setBounds(0, 0,
(bounds.width < maxWidth) ? bounds.width : maxWidth,
(bounds.height < maxHeight) ? bounds.height : maxHeight);
}
frame.setLocationRelativeTo(null);
Here, maxWidth and maxHeight come from getScreenSize() as above. It
works -- sort of. When the packed frame tries to be too large, the
above code forces it to fit on the screen, without enlarging any
dimension. And, as hoped, the lost space is taken from the
JScrollPane, which then displays its scrollbars and provides a
scrolling view on the huge contents.
Unfortunately, the above does not quite work perfectly because it
always shrinks both dimensions of the JFrame, even if one was less
than its maximum! In particular, this implies that
frame.setBounds(frame.getBounds()) would not be a no-op but would
shrink the frame slightly. The shrinkage is only 10-20 pixels or so in
each dimension, but it's troublesome.
Either getBounds() is not reporting the true size of the frame, or
setBounds() trims some off.
Any suggestions on how to get rid of this shrinkage, so that each
dimension is the smaller of the dimension it has immediately after
pack() or maxFoo?
When this happened, the window would end up larger than the screen, so
I shoehorned everything into a JScrollPane, which didn't make any
difference. I then tried
frame.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
without any effect. Apparently JFrame ignores its own maximum size
when pack() is called.
Which led to:
frame.pack();
Rectangle bounds = frame.getBounds();
if (bounds.width > maxWidth || bounds.height > maxHeight) {
frame.setBounds(0, 0,
(bounds.width < maxWidth) ? bounds.width : maxWidth,
(bounds.height < maxHeight) ? bounds.height : maxHeight);
}
frame.setLocationRelativeTo(null);
Here, maxWidth and maxHeight come from getScreenSize() as above. It
works -- sort of. When the packed frame tries to be too large, the
above code forces it to fit on the screen, without enlarging any
dimension. And, as hoped, the lost space is taken from the
JScrollPane, which then displays its scrollbars and provides a
scrolling view on the huge contents.
Unfortunately, the above does not quite work perfectly because it
always shrinks both dimensions of the JFrame, even if one was less
than its maximum! In particular, this implies that
frame.setBounds(frame.getBounds()) would not be a no-op but would
shrink the frame slightly. The shrinkage is only 10-20 pixels or so in
each dimension, but it's troublesome.
Either getBounds() is not reporting the true size of the frame, or
setBounds() trims some off.
Any suggestions on how to get rid of this shrinkage, so that each
dimension is the smaller of the dimension it has immediately after
pack() or maxFoo?