what's the difference between DEFAULT_SIZE and PREFERRED_SIZE?
Asked Answered
G

2

7

I'm using Swing GroupLayout and I'm confused about the values GroupLayout.DEFAULT_SIZE and GroupLayout.PREFERRED_SIZE. I never know when to use each one of them in methods like GroupLayout.addComponent(Component, int, int, int).

suppose I have this code:

GroupLayout l = ...;

l.setHorizontalGroup(l.createSequentialGroup()
    .addComponent(tf1)
    .addComponent(tf2));

l.setVerticalGroup(l.createParallelGroup()
    .addComponent(tf1)
    .addComponent(tf2));

there are two JTextFields on a single line laid out with GroupLayout (one sequential group horizontally and one parallel group vertically). if I resize the window now, both components get the available space (50% each). but I want only the first text field to grow/shrink horizontally and only the second text field to grow/shrink vertically. what values of min, pref and max should I use to accomplish that? I know I can just try it and see what combination works but I'd like to know the reasoning behind this problem.

Grochow answered 1/4, 2010 at 20:21 Comment(2)
Basically GroupLayout is designed to be used in WYSIWYG user interface editors (like in Netbeans). Do you ask out od curiosity or you use it "by hand"?Heliopolis
both :-) I use the WYSIWYG editor from NetBeans but I like to understand the code so I can implement some simple cases 'by hand'. I have more control of the code when I do it myself.Grochow
F
4

Some guidance may be found in How to Use GroupLayout: Component Size and Resizability. Regarding DEFAULT_SIZE and PREFERRED_SIZE,

They can be used as parameters in the method

 addComponent(Component comp, int min, int pref, int max)

To force a component to be resizable (allow shrinking and growing):

 group.addComponent(component, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

This allows the component to resize between zero size (minimum) to any size (Short.MAX_VALUE as maximum size means "infinite"). If we wanted the component not to shrink below its default minimum size, we would use GroupLayout.DEFAULT_SIZE instead of 0 in the second parameter.

To make a component fixed size (suppress resizing):

 group.addComponent(component, GroupLayout.PREFERRED_SIZE,
     GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)...

Interestingly, the constant values are negative, so they cannot be mistaken for actual constraints.

Fraxinella answered 2/4, 2010 at 1:11 Comment(0)
R
1

I was also confused about how GroupLayout.DEFAULT_SIZE and GroupLayout.PREFERRED_SIZE are used in GroupLayout.SequentialGroup.addComponent(Component c, int min, int pref, int max), even after referencing the GroupLayout section of the Java Tutorials and a java.net article titled Getting to know GroupLayout, part 1.

Diving into the JDK 1.6.0_27 GroupLayout.javasource I found the answers in the ComponentSpring class. From that I was able to work out these rules:

If a minimum size is needed:

  1. and the provided min value is non-negative, that value is returned.
  2. else if it is PREFERRED_SIZE, we follow the rules for preferred size.
  3. else the component's minimum size is returned.

If a preferred size is needed:

  1. and the provided pref value is non-negative, that value is returned.
  2. else if it is DEFAULT_SIZE or PREFERRED_SIZE, return the component's preferred size.

If a maximum size is needed:

  1. and the provided max value is non-negative, that value is returned.
  2. else if it is PREFERRED_SIZE, we follow the rules for preferred size.
  3. else the component's maximumsize is returned.

As trashgod already noted, the defines are negative. Any other negative value for min, pref and max besides DEFAULT_SIZE or PREFERRED_SIZE are errors and hit asserts.

The interplay between the SequentialGroup.addComponent min, pref, and max sizes wasn't immediately apparent to me from the tutorial. Now I know why PREFERRED_SIZE,DEFAULT_SIZE,PREFERRED_SIZE is fixed, why it doesn't matter if the middle argument is DEFAULT_SIZE or PREFERRED_SIZE and how the NetBeans generated fixed size values like DEFAULT_SIZE,300,Short.MAX_VALUE work.

Richerson answered 13/1, 2012 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.