Preferred Size, Minimum Size, Size

Discussion in 'Java' started by Jason Cavett, May 23, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    I'm a little confused when I should use the various JComponent
    methods: setPreferredSize, setMinimumSize and setSize. From what I
    can tell:

    setPreferredSize(Dimension) - This is (obviously) the preferred size
    of the component. As far as I can tell, this is used to determine
    what size the component should be when pack() is used.

    setMinimumSize - The component cannot be any smaller than this.

    setSize - Set the current size (hard value).


    What I'm confused about is...
    1. When I should use each method.
    2. Currently, when I resize a frame, the JTextBoxes will resize for
    part of the time and then, as the JFrame gets smaller, they will
    collapse to an extremely small size (1 character wide...if that). Do
    I *have* to use setMinimumSize to prevent this from happening?/


    Thanks for any info. Appreciate it.
    Jason Cavett, May 23, 2008
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > I'm a little confused when I should use the various JComponent
    > methods: setPreferredSize, setMinimumSize and setSize. From what I
    > can tell:
    >
    > setPreferredSize(Dimension) - This is (obviously) the preferred size
    > of the component. As far as I can tell, this is used to determine
    > what size the component should be when pack() is used.
    >
    > setMinimumSize - The component cannot be any smaller than this.
    >
    > setSize - Set the current size (hard value).
    >
    >
    > What I'm confused about is...
    > 1. When I should use each method.
    > 2. Currently, when I resize a frame, the JTextBoxes will resize for
    > part of the time and then, as the JFrame gets smaller, they will
    > collapse to an extremely small size (1 character wide...if that). Do
    > I *have* to use setMinimumSize to prevent this from happening?/
    >
    >
    > Thanks for any info. Appreciate it.


    The answer is it depends a lot on the layout manager that you are using.

    The collapsing into a one pixel size makes me think that you are using
    GridBagLayout. GridBagLayout can be configured to pay attention to
    preferred and minimum size. I just tried it with my GridBagLayout
    Tester JApplet and it appears not to honor maximum size.

    So assuming you are using GridBagLayout the answer is yes you have to
    set the minimum size. But remember if you shrink the container beyond
    that which will hold the components at minimum size, they will disappear.

    Feel free to play with my GridBagLayout Tester JApplet

    http://rabbitbrush.frazmtn.com/gridbagtester.html

    --

    Knute Johnson
    email s/knute/nospam/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, May 23, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Roedy Green Guest

    Roedy Green, May 23, 2008
    #3
  4. Jason Cavett

    Jason Cavett Guest

    On May 23, 1:22 pm, Knute Johnson <>
    wrote:
    > Jason Cavett wrote:
    > > I'm a little confused when I should use the various JComponent
    > > methods: setPreferredSize, setMinimumSize and setSize.  From what I
    > > can tell:

    >
    > > setPreferredSize(Dimension) - This is (obviously) the preferred size
    > > of the component.  As far as I can tell, this is used to determine
    > > what size the component should be when pack() is used.

    >
    > > setMinimumSize - The component cannot be any smaller than this.

    >
    > > setSize - Set the current size (hard value).

    >
    > > What I'm confused about is...
    > > 1. When I should use each method.
    > > 2. Currently, when I resize a frame, the JTextBoxes will resize for
    > > part of the time and then, as the JFrame gets smaller, they will
    > > collapse to an extremely small size (1 character wide...if that).  Do
    > > I *have* to use setMinimumSize to prevent this from happening?/

    >
    > > Thanks for any info.  Appreciate it.

    >
    > The answer is it depends a lot on the layout manager that you are using.
    >
    > The collapsing into a one pixel size makes me think that you are using
    > GridBagLayout.  GridBagLayout can be configured to pay attention to
    > preferred and minimum size.  I just tried it with my GridBagLayout
    > Tester JApplet and it appears not to honor maximum size.
    >
    > So assuming you are using GridBagLayout the answer is yes you have to
    > set the minimum size.  But remember if you shrink the container beyond
    > that which will hold the components at minimum size, they will disappear.
    >
    > Feel free to play with my GridBagLayout Tester JApplet
    >
    > http://rabbitbrush.frazmtn.com/gridbagtester.html
    >
    > --
    >
    > Knute Johnson
    > email s/knute/nospam/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >       ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


    That's a nice tool. Thanks, Knute.
    Jason Cavett, May 23, 2008
    #4
  5. On 2008-05-23 17:34 +0100, Jason Cavett allegedly wrote:
    > I'm a little confused when I should use the various JComponent
    > methods: setPreferredSize, setMinimumSize and setSize. From what I
    > can tell:
    >
    > setPreferredSize(Dimension) - This is (obviously) the preferred size
    > of the component. As far as I can tell, this is used to determine
    > what size the component should be when pack() is used.
    >
    > setMinimumSize - The component cannot be any smaller than this.
    >
    > setSize - Set the current size (hard value).
    >
    >
    > What I'm confused about is...
    > 1. When I should use each method.
    > 2. Currently, when I resize a frame, the JTextBoxes will resize for
    > part of the time and then, as the JFrame gets smaller, they will
    > collapse to an extremely small size (1 character wide...if that). Do
    > I *have* to use setMinimumSize to prevent this from happening?/
    >
    >
    > Thanks for any info. Appreciate it.


    .... you forgot MaximumSize in the lot.

    What others said notwithstanding, minimum, preferred and maximum size
    come into use especially when you're designing your own components, as a
    means to indicate how they ought to behave in LayoutManagers. Legacy
    components usually already have a specific and more or less definite
    behaviour with regards to that.

    --
    DF.
    to reply privately, change the top-level domain
    in the FROM address from "invalid" to "net"
    Daniele Futtorovic, May 23, 2008
    #5
  6. Jason Cavett

    Michael Jung Guest

    Daniele Futtorovic <> writes:
    > On 2008-05-23 17:34 +0100, Jason Cavett allegedly wrote:
    >> I'm a little confused when I should use the various JComponent
    >> methods: setPreferredSize, setMinimumSize and setSize. From what I
    >> can tell:
    >>
    >> setPreferredSize(Dimension) - This is (obviously) the preferred size
    >> of the component. As far as I can tell, this is used to determine
    >> what size the component should be when pack() is used.
    >>
    >> setMinimumSize - The component cannot be any smaller than this.
    >>
    >> setSize - Set the current size (hard value).
    >>
    >> What I'm confused about is...
    >> 1. When I should use each method.
    >> 2. Currently, when I resize a frame, the JTextBoxes will resize for
    >> part of the time and then, as the JFrame gets smaller, they will
    >> collapse to an extremely small size (1 character wide...if that). Do
    >> I *have* to use setMinimumSize to prevent this from happening?/
    >>
    >> Thanks for any info. Appreciate it.

    >
    > ... you forgot MaximumSize in the lot.
    >
    > What others said notwithstanding, minimum, preferred and maximum size
    > come into use especially when you're designing your own components, as a
    > means to indicate how they ought to behave in LayoutManagers. Legacy
    > components usually already have a specific and more or less definite
    > behaviour with regards to that.


    As a rule of thumb, which hasn't betrayed me so far, consider that the
    getters are called by the layout managers and the results are
    honoured. That does not preclude components vanishing (behind window
    boundaries) or being oversized (filled with background from the parent
    component). The setters are there for symmetry. But the values you
    set may or may not be persistent, this is at the discretion of the
    component you use it on. The reason is that components often
    calculate their sizes depening on their contents, so if their content
    changes, their (min, max, pref, actual) sizes change and whatever you
    set may become lost - this is most obvious for the actual/current size
    and shows that this also depends on the layout of the parent (and
    thereby the brothers and sisters of your components). The best thing
    to do is to inherit from your favorite component and implement the
    getters to suite your needs. (Use anonymous classes, if that is all
    you want to change.) Then the layout only depends on how respectful
    the layout manager is.

    Michael
    Michael Jung, May 25, 2008
    #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. Kerry Shetline

    Component preferred size dilemma

    Kerry Shetline, Nov 13, 2003, in forum: Java
    Replies:
    4
    Views:
    3,333
    Babu Kalakrishnan
    Dec 5, 2003
  2. Shiperton Henethe
    Replies:
    117
    Views:
    2,145
    Andy Dingley
    Dec 22, 2004
  3. Chris F.A. Johnson

    Re: Preferred way to size fonts

    Chris F.A. Johnson, Nov 5, 2008, in forum: HTML
    Replies:
    19
    Views:
    753
    Sherm Pendley
    Nov 8, 2008
  4. dorayme

    Re: Preferred way to size fonts

    dorayme, Nov 5, 2008, in forum: HTML
    Replies:
    0
    Views:
    425
    dorayme
    Nov 5, 2008
  5. richard

    Re: Preferred way to size fonts

    richard, Nov 6, 2008, in forum: HTML
    Replies:
    6
    Views:
    465
    Acmeous
    Nov 10, 2008
Loading...

Share This Page