View unanswered posts | View active topics It is currently Tue Feb 21, 2017 5:52 am

Reply to topic  [ 3 posts ] 
 layout(changed) does not clear the MigLayout cache 
Author Message

Joined: Wed Jun 15, 2011 10:37 am
Posts: 3
Post layout(changed) does not clear the MigLayout cache
In the following code snippet I create a Composite "container" and some children. The Action "refresh" disposes of all children and recreates them in the same Composite. Calling "container".layout(true) will throw a SWTException "Widget is disposed". Digging in the MigLayout source you will find that the implementation of MigLayout.layout(Composite parent, boolean flushCache) uses its own method "checkCache(Composite)" to determine whether or not to clear the cache instead of relying on the second argument "flushCache".

This problem cannot be fixed by using "nocache" in the MigLayout Constructor but can be worked around by calling "container".changed("container".getCHildren()) before calling layout(). The MigLayout implementation of changed() will call flushCache() at some point and after that layouting will work properly. in my point of view this classifies as a bug. By ignoring the parameter "flushCache" the implementation of checkCache() must yield the same result as "flushCache" demands. What do you think?

Here is the code snippet:

import java.util.Date;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class CachingBug {

  public static void main(final String[] args) {
    final Display display = new Display();

    final Shell shell = new Shell(display);
    shell.setLayout(new MigLayout("wrap 1", "", ""));

    final Composite container = new Composite(shell, SWT.BORDER);
    container.setLayout(new MigLayout("wrap 1"));

    final Button refreshButton = new Button(shell, SWT.PUSH);
    refreshButton.addMouseListener(new MouseAdapter() {
      public void mouseUp(final MouseEvent event) {
        for (final Control control : container.getChildren()) {
        // container.changed(container.getChildren());

    shell.setSize(300, 300);;
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {

  private static void createLabels(final Composite container) {
    final String date = new Date().toString();
    for (int i = 0; i < 5; i++) {
      new Label(container, SWT.NONE).setText("My Text " + i + " - " + date);


Wed Feb 08, 2012 11:59 am
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: layout(changed) does not clear the MigLayout cache
Sorry for missing this.

I have fixed by always flushing the cache if the flushCache is true. The fix will be in the next version.


Thu Feb 21, 2013 12:20 am
Profile WWW

Joined: Wed Jun 15, 2011 10:37 am
Posts: 3
Post Re: layout(changed) does not clear the MigLayout cache
Dear Mikael,

thanks for your reply and the fix.

Unfortunately, we switched to using GridLayout consistently which is more reliable.


Thu Feb 21, 2013 4:45 pm
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.