Enforce minimum window size


E

electric sheep

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?
 
Ad

Advertisements

A

Alan Moore

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());
}

}
 
E

electric sheep

This guy at jgoodies really knows how to make a nice gui.

I'm curious ... you have any idea what components he is using to get the
"window within a window" look ?

Many of his applications are "divided" up by these cool "inner windows".
They look like JInternalFrames that cannot be sized, moved, closed, etc
....

Either that or they are some sort of cool custom border with title
bars.
 
L

Liz

electric sheep said:
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 method componentResized() is called when the window is resized,
but maybe it is too late, or maybe you can reset it to what you want.
Here is what I do.
-------
public void componentResized(ComponentEvent e)
{
Component c = e.getComponent();
int w = c.getSize().width;
int h = c.getSize().height;
int x = c.getX();
int y = c.getY();
System.out.println("Component " + c.getClass().getName() + " was resized
new width = " + w + ", new height = " + h);
MyClass.HandleWindowResize(x, y, h, w);
}
 
Ad

Advertisements

A

Andrew Thompson

This guy at jgoodies really knows how to make a nice gui.

Karsten certainly does. He is a regular contibutor
to c.l.j.gui and occasionally pops in to c.l.j.p.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top