What is the difference between DEFAULT_SIZE and PREFERRED_SIZE?

I am using Swing GroupLayout and I am confused about the values โ€‹โ€‹of GroupLayout.DEFAULT_SIZE and GroupLayout.PREFERRED_SIZE . I never know when to use each of them in methods such as 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 JTextField on the same line laid out using GroupLayout (one consecutive horizontal group and one parallel vertical group). if I resize the window now, both components will get the available space (50% each). but I want only the first text field to increase / decrease horizontally, and only the second text field to increase / decrease vertically. What values โ€‹โ€‹of min, pref and max should be used for this? I know that I can just try and see which combination works, but I would like to know the cause of this problem.

+6
java resize swing size grouplayout
source share
2 answers

Some recommendations can be found in How to Use GroupLayout: Size and Dimension of Components . Regarding DEFAULT_SIZE and PREFERRED_SIZE ,

They can be used as parameters in a method.

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

To force an object to resize (allow compression and growth):

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

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

To make a component's 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 limitations.

+3
source share

I was also confused by the way GroupLayout.DEFAULT_SIZE and GroupLayout.PREFERRED_SIZE are used in GroupLayout.SequentialGroup.addComponent(Component c, int min, int pref, int max) , even after linking to the GroupLayout section in Java tutorials and the java.net article under titled Introducing GroupLayout, Part 1 .

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

If a minimum size is required:

  • and the provided min value is non-negative, this value is returned.
  • else if it is PREFERRED_SIZE , we follow the rules for the preferred size.
  • otherwise, the minimum component size is returned.

If preferred size is required:

  • and the provided pref value is non-negative, this value is returned.
  • else, if it is DEFAULT_SIZE or PREFERRED_SIZE , return the preferred component size.

If maximum size is required:

  • and the provided max value is non-negative, this value is returned.
  • else if it is PREFERRED_SIZE , we follow the rules for the preferred size.
  • otherwise, the maximum size of the component is returned.

As already noted, trashgod determines negative. Any other negative value for min, pref, and max other than DEFAULT_SIZE or PREFERRED_SIZE is an error and raises assertions.

The interaction between the sizes of SequentialGroup.addComponent min, pref, and max did not immediately appear to me from the tutorial. Now I know why PREFERRED_SIZE,DEFAULT_SIZE,PREFERRED_SIZE fixed, why it does not matter if the middle argument is DEFAULT_SIZE or PREFERRED_SIZE and how NetBeans fixed size values โ€‹โ€‹such as DEFAULT_SIZE,300,Short.MAX_VALUE .

+1
source share

All Articles