Preferred Size, Minimum Size, Size


J

Jason Cavett

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

Advertisements

K

Knute Johnson

Jason said:
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
 
J

Jason Cavett

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

That's a nice tool. Thanks, Knute.
 
D

Daniele Futtorovic

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

Advertisements

M

Michael Jung

Daniele Futtorovic said:
... 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
 
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top