Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Enforce minimum window size

Reply
Thread Tools

Enforce minimum window size

 
 
electric sheep
Guest
Posts: n/a
 
      05-19-2004
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?
 
Reply With Quote
 
 
 
 
Alan Moore
Guest
Posts: n/a
 
      05-20-2004
On Wed, 19 May 2004 22:22:08 GMT, electric sheep
<(E-Mail Removed)> 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());
}

}

 
Reply With Quote
 
 
 
 
electric sheep
Guest
Posts: n/a
 
      05-20-2004
THanks for the tip.

e
 
Reply With Quote
 
electric sheep
Guest
Posts: n/a
 
      05-20-2004
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.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      05-21-2004

"electric sheep" <(E-Mail Removed)> wrote in message
news:k6Rqc.15799861$(E-Mail Removed).. .
> 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);
}


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-21-2004
On Thu, 20 May 2004 22:59:13 GMT, electric sheep wrote:

> 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.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
minimum size for a jframe Brandon McCombs Java 2 03-28-2006 08:44 AM
the minimum size when using strncpy(...) Simon C++ 3 09-06-2004 01:03 PM
Minimum pixel size Alfred Molon Digital Photography 25 08-02-2004 10:26 PM
Wizard, property sheet and minimum size... luc h C++ 3 01-09-2004 02:36 PM



Advertisments