On Wed, 19 May 2004 22:22:08 GMT, electric sheep
<> wrote:
>Hi,
>i would like to have a JFrame that cannot be resized below a certain
>minimum value which i set.
>
>Is this a window, component, or layout manager issue ?
>
>It seems like most layout managers i use allow me to size my window down
>as small as i like ... which causes all my components to "act crazy".
>
>It seems like setting the preferredSize doesn't really matter.
>
>I can size my app all the way down to just the titlebar if i like.
>
>Do i need to extend Window or Component or JFrame maybe ? and override some sort
>of resize method ?
>
>I can't figure out what method is called on what object when somebody
>resizes a JFrame.
>
>My guess is that the layout manager only controls the layout INSIDE the
>container (the content pane, say) ... but it cannot prevent the
>container from being resized below a certain threshold.
>
>Anyhow, any thoughts on this ?
>
>Also, my friend told me if your a control freak you should probably be
>using GridBadLayout. Is this true ?
>How about this new SpringLayout ?
>
>Anybody know any really good open source layout managers available on the web?
The best layout manager is JGoodies Forms:
https://forms.dev.java.net/
But you're right: layout managers can't enforce a minimum size--and
Swing doesn't provide any other way to do it, either. You can roll
your own with a ComponentListener, but it's not totally satisfactory;
instead of preventing the user from sizing the window too small, you
can only snap it back to the minumum size once the user lets go.
I ran into this problem when I wanted my Search/Replace dialog to be
resizable horizontally, but not below a minimum width. I wanted it to
have a fixed height but, since Since Swing doesn't offer separate
settings for vertical and horizontal resizability, I had to handle
that as well. I extended JDialog and added the following inner class,
which I create and attach after the dialog has been populated and
pack() has been called.
class ComponentHandler extends ComponentAdapter
{
Dimension minSize = getMinimumSize();
Rectangle bounds = getBounds();
public void componentResized(ComponentEvent evt)
{
/*
* Let the user stretch the dialog horizontally, to make the
* entry fields bigger, but if they make it too narrow, or
* try to resize it vertically, snap it back to its preferred
* size. If the user is dragging on the top or left border,
* the system counts it as a move as well as a resize; we
* have to explicitly restore the origin position so that the
* user doesn't end up chasing the dialog around the screen
* like a drop of mercury.
*/
int oldX = bounds.x;
int oldY = bounds.y;
int oldHeight = bounds.height;
int newX = getX();
int newWidth = getWidth();
if (newWidth < minSize.width || getHeight() != oldHeight)
{
int diff = minSize.width - newWidth;
if (diff > 0 && newX != oldX)
{
newX -= diff;
}
newWidth += Math.max(0, diff);
setBounds(newX, oldY, newWidth, oldHeight);
}
bounds.setBounds(newX, oldY, newWidth, oldHeight);
}
public void componentMoved(ComponentEvent evt)
{
/*
* Store the dialog's new location if the user moved it by
* dragging the title bar, but not if the move event was a
* side effect of resizing.
*/
if (getWidth() != bounds.width || getHeight() != bounds.height)
{
setBounds(bounds);
return;
}
bounds.setLocation(getX(), getY());
}
}