[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] layout() Doesn't Refresh Composite

Hi,

I think I'm missing something pretty fundamental here... Because everywhere I look people are saying that calling layout() will "redraw" your custom composite. However, when I do that, nothing happens. I've tried many examples from the SWT Snippets and JavaDoc, and they work fine... Maybe someone can give me a much needed tip?

I've created a custom widget, that extends ScrolledComposite. I create a composite on which I put my basic widgets and then set "this" (this being a ScrolledComposite) content to the composite. Most of my basic widgets are just some canvas' and they get set to a default size at first. Now, the user of my custom widget can set the size of the canvas' to something other than the default. Thus, when they change the size of the canvas' I want my widget to layout again with the new sizes. But that doesn't happen.

Below is a runnable code snippet.

Thanks,
Kevin


import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;

public class CustomWidgetExample {

public CustomWidgetExample() {}

public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FormLayout());
shell.setSize(425, 200);
CustomWidgetExample cwe = new CustomWidgetExample();
CustomWidget customWidget = cwe.new CustomWidget(shell, SWT.NONE);
customWidget.setItemSize(new Point(60, 45));
final FormData fd_custom = new FormData();
fd_custom.top = new FormAttachment(0, 5);
fd_custom.right = new FormAttachment(0, 400);
fd_custom.left = new FormAttachment(0, 5);
customWidget.setLayoutData(fd_custom);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();

}

class CustomWidget extends ScrolledComposite {

private static final int DEFAULT_MAX_PHOTOS = 50;
private static final int DEFAULT_ITEM_WIDTH = 10/*60*/;
private static final int DEFAULT_ITEM_HEIGHT = 10/*45*/;

private Composite _container;
private Canvas[] _items;
private int _itemCount = DEFAULT_MAX_PHOTOS;
private int _itemWidth = DEFAULT_ITEM_WIDTH;
private int _itemHeight = DEFAULT_ITEM_HEIGHT;

public CustomWidget(Composite parent, int style) {
super(parent, style | SWT.H_SCROLL);
createContents();
}

private void createContents() {
_container = new Composite(this, SWT.NONE);
setContent(_container);
_container.setLayout(new FormLayout());

//Filmstrip of canvas' to draw the items onto.
_items = new Canvas[_itemCount];
for (int i = 0; i < _items.length; i++) {

final Canvas item = new Canvas(_container, SWT.BORDER);
FormData fd_item = new FormData();

if (i == 0) {

//drawing first item
fd_item.top = new FormAttachment(0, 5);
fd_item.bottom = new FormAttachment(0, _itemHeight + 5);
fd_item.left = new FormAttachment(0, 6);
fd_item.right = new FormAttachment(0, _itemWidth + 6);

} else {

//drawing the rest of the items
Canvas prevItem = _items[i-1];
fd_item.bottom = new FormAttachment(prevItem, _itemHeight, SWT.TOP);
fd_item.top = new FormAttachment(prevItem, 0, SWT.TOP);
fd_item.left = new FormAttachment(prevItem, 6, SWT.RIGHT);
fd_item.right = new FormAttachment(prevItem, _itemWidth + 6, SWT.RIGHT);

}
item.setLayoutData(fd_item);

_items[i] = item;

}

//Set the size of this scrolled composite.
_container.setSize(_container.computeSize(SWT.DEFAULT, SWT.DEFAULT));

}

public void setItemSize(Point size) {
_itemWidth = size.x;
_itemHeight = size.y;

_container.setSize(_container.computeSize(SWT.DEFAULT, SWT.DEFAULT));
_container.layout();
}

}


}