I'm using MiGLayout and being the freak i am, i was thinking if it's possible to create a single instance of the layout and use it for all my panels? Just curious...
+2
A:
That depends entirely on whether the layout manager retains data related to layout. Generally, any layout manager that takes constraints needs to store those, so it must be instantiated one-to-one with the container.
With particular reference to MigLayout, look at how much data it stores that would not be applicable to multiple containers:
public final class MigLayout implements LayoutManager2, Externalizable
{
// ******** Instance part ********
/** The component to string constraints mappings.
*/
private final Map<Component, Object> scrConstrMap = new IdentityHashMap<Component, Object>(8);
/** Hold the serializable text representation of the constraints.
*/
private Object layoutConstraints = "", colConstraints = "", rowConstraints = ""; // Should never be null!
// ******** Transient part ********
private transient ContainerWrapper cacheParentW = null;
private transient final Map<ComponentWrapper, CC> ccMap = new HashMap<ComponentWrapper, CC>(8);
private transient javax.swing.Timer debugTimer = null;
private transient LC lc = null;
private transient AC colSpecs = null, rowSpecs = null;
private transient Grid grid = null;
private transient int lastModCount = PlatformDefaults.getModCount();
private transient int lastHash = -1;
private transient Dimension lastInvalidSize = null;
private transient ArrayList<LayoutCallback> callbackList = null;
private transient boolean dirty = true;
Software Monkey
2010-03-19 18:07:45
A:
Some layout managers will only work with one Container
(such as BoxLayout
). My guess is that there's little benefit gained by trying to reuse the same layout manager.
Steve Kuo
2010-03-19 23:54:05