Pack, but don't make it smaller
Asked Answered
S

3

9

I have JFrame with GridBagLayout. User can resize this window. Also, he can perform some editing actions that change window size. I use pack(); repaint(); now after such actions. But, actually I shouldn't make window smaller after such operations - only bigger. What I found as solution is

    Dimension oldSize = getSize();
    pack();
    Dimension newSize = window.getSize();
    setSize(
            (int) Math.max(newSize.getWidth(), oldSize.getWidth()),
            (int) Math.max(newSize.getHeight(), oldSize.getHeight()));
    repaint();

But I don't like this solution at all. Beside ugly code, we set size twice (once by pack and than directly). Is there any other solutions?

Showker answered 26/6, 2011 at 17:50 Comment(4)
What about setMinimumSize before pack?Simplify
@Paŭlo Ebermann , thanks a lot. It was what I need. Please post it as answer.Showker
Call validate() instead of pack().Aerostation
@Andrew Thompson , unfortunately it doesn't work as I want.Showker
G
14

A simple solution would be to use something like this:

frame.setMinimumSize(frame.getSize());
frame.pack();
frame.setMinimumSize(null);

This will not allow pack() to make the window smaller, only bigger, I think. By resetting the minimum size to null after the pack() we avoid preventing the user on resizing it smaller afterwards.

You should not need a repaint() at the end, the size changing should trigger a repaint by itself. (Of course, make sure that all these actions happen in the event dispatch thread.)

Grillparzer answered 26/6, 2011 at 18:4 Comment(1)
On Ubuntu, this causes my form to move 10-20px up (that's wierd).Eustazio
L
2

An alternative solution to the one proposed by Paŭlo is the following code:

private boolean greater(Dimension left, Dimension right) {
    return left.getHeight() > right.getHeight() || left.getWidth() > right.getWidth();
}

if (greater(window.getPreferredSize(), window.getSize())) {
    window.pack();
}

The advantage of this solution is that pack isn't called unless it is necessary, and it avoids a flicker we observed on Linux with Paŭlo's solution.

Lethe answered 10/9, 2015 at 15:32 Comment(0)
V
0

You could override the pack() method to do that. Not sure if there's a better way though.

Vip answered 26/6, 2011 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.