Enforce minimum window size

Discussion in 'Java' started by electric sheep, May 19, 2004.

  1. 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?
    electric sheep, May 19, 2004
    #1
    1. Advertising

  2. electric sheep

    Alan Moore Guest

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

    }
    Alan Moore, May 20, 2004
    #2
    1. Advertising

  3. THanks for the tip.

    e
    electric sheep, May 20, 2004
    #3
  4. 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.
    electric sheep, May 20, 2004
    #4
  5. electric sheep

    Liz Guest

    "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);
    }
    Liz, May 21, 2004
    #5
  6. 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
    Andrew Thompson, May 21, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brandon McCombs

    minimum size for a jframe

    Brandon McCombs, Mar 27, 2006, in forum: Java
    Replies:
    2
    Views:
    3,559
    Alex Hunsley
    Mar 28, 2006
  2. luc h
    Replies:
    3
    Views:
    520
    Victor Bazarov
    Jan 9, 2004
  3. Simon
    Replies:
    3
    Views:
    702
    Simon
    Sep 6, 2004
  4. cpp_novice

    C90: minimum size of long

    cpp_novice, Jul 19, 2006, in forum: C Programming
    Replies:
    4
    Views:
    448
    Dik T. Winter
    Jul 19, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,556
    Michael Jung
    May 25, 2008
Loading...

Share This Page