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
<> 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" <> wrote in message
news:k6Rqc.15799861$.. .
> 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 Off
Pingbacks are Off
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 of void * Rob Hoelz C Programming 24 09-13-2007 09:15 AM
C90: minimum size of long cpp_novice C Programming 4 07-19-2006 10:57 PM
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



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57