f



Preferred Size, Minimum Size, Size

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.
0
jason.cavett (211)
5/23/2008 3:34:50 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

5 Replies
1569 Views

Similar Articles

[PageSpeed] 41

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
0
nospam8071 (1252)
5/23/2008 5:22:41 PM
On Fri, 23 May 2008 08:34:50 -0700 (PDT), Jason Cavett
<jason.cavett@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>setPreferredSize(Dimension) 

see http://mindprod.com/jgloss/getpreferredsize.html
-- 

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
see_website (5876)
5/23/2008 5:56:54 PM
On May 23, 1:22=A0pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> Jason Cavett wrote:
> > I'm a little confused when I should use the various JComponent
> > methods: setPreferredSize, setMinimumSize and setSize. =A0From what I
> > can tell:
>
> > setPreferredSize(Dimension) - This is (obviously) the preferred size
> > of the component. =A0As 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). =A0Do
> > I *have* to use setMinimumSize to prevent this from happening?/
>
> > Thanks for any info. =A0Appreciate 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. =A0GridBagLayout can be configured to pay attention to
> preferred and minimum size. =A0I 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. =A0But 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
> =A0 =A0 =A0 ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

That's a nice tool.  Thanks, Knute.
0
jason.cavett (211)
5/23/2008 7:38:22 PM
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"
0
5/23/2008 10:18:09 PM
Daniele Futtorovic <da.futt.news@laposte.invalid> 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
0
miju1 (27)
5/25/2008 8:32:46 AM
Reply: