FaqMatisseCustomLayoutStyle

Can I define my own spacing scheme (default gaps between components)?

Not directly in the GUI builder. You can just specify individual gap sizes via "Space Around Component" dialog invoked from the context menu.

However, in source code you can register your own LayoutStyle class implementation and provide the size of the default gaps as you like.

The LayoutStyle class contains two methods you are interested in: getPreferredGap and getContainerGap. The first one is used to determine a preferred distance between two neighbouring components, the second one provides the preferred distance between a component and the container border. Instances of the components are passed to these methods as arguments.

It is on you to decide how you do your custom spacing - what the distance should be for actual components. You can delegate to the default implementation if given components don't match your special criteria. The default implementation provides spaces according to the actual look and feel's recommended guidelines.

Unless you don't use JDK 6, you need the Swing Layout Extension library to create a custom spacing scheme with older JDK versions. You can choose between those two libraries in the properties window of your form (Inspector -> select your Form -> .click right + properties -> choose 'Layout Generation Style'). Note that they have minor API differences.

The custom LayoutStyle class implementation, including the delagation to the default implementation, might look like this:

'Standard Java 6' Layout Generation (since JDK 6)

import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.LayoutStyle;

public class MyLayoutStyle extends LayoutStyle {

    private static LayoutStyle instance;
    private static LayoutStyle originalStyle;

    public static void register() {
        if (instance == null) {
            instance = new MyLayoutStyle();
            originalStyle = LayoutStyle.getInstance();
            LayoutStyle.setInstance(instance);
        }
    }

    public int getPreferredGap(JComponent component1, JComponent component2, ComponentPlacement type, int position, Container parent) {
        // put your own condition here
        if (component1 instanceof MyComponent && component2 instanceof MyComponent) {
            switch (type) {
                case RELATED:
                    return 6;
                case UNRELATED:
                    return 16;
                case INDENT:
                    return 12;
            }
        }
        return originalStyle.getPreferredGap(component1, component2, type, position, parent);
    }

    public int getContainerGap(JComponent component, int position, Container parent) {
        if (parent instanceof MyContainer) // put your own condition here
        {
            return 20;
        }

        return originalStyle.getContainerGap(component, position, parent);
    }

}

'Swing Layout Extensions Library' Layout Generation (older JDKs)

import java.awt.Container;
import javax.swing.JComponent;
import org.jdesktop.layout.LayoutStyle;

public class MyLayoutStyle extends LayoutStyle {

    private static LayoutStyle instance;
    private static LayoutStyle originalStyle;

    public static void register() {
        if (instance == null) {
            instance = new MyLayoutStyle();
            originalStyle = LayoutStyle.getSharedInstance();
            LayoutStyle.setSharedInstance(instance);
        }
    }

    public int getPreferredGap(JComponent component1, JComponent component2,
                               int type, int position, Container parent) {
        // put your own condition here
        if (component1 instanceof MyComponent && component2 instanceof MyComponent) {
            switch (type) {
                case RELATED: return 6;
                case UNRELATED: return 16;
                case INDENT: return 12;
            }
        }
        return originalStyle.getPreferredGap(component1, component2, type, position, parent);
    }

    public int getContainerGap(JComponent component, int position, Container parent) {
        if (parent instanceof MyContainer) // put your own condition here
            return 20;

        return originalStyle.getContainerGap(component, position, parent);
    }
}


To register the class just call MyLayoutStyle.register() before the layout is done.

Note:

  • You can't unregister (it's a bit tricky...)
  • If you don't care about the default implementation you can have the class simpler (without the delegation stuff).
  • You'll see your spacing only when executing the application, but not in the GUI builder.
  • For more details look at the javadoc which is available for the swing-layout library in NetBeans or visit the http://swing-layout.dev.java.net and download the sources.

  Applies to: NetBeans 5.0, 5.5, 6.x
  Platforms: All
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo