简体   繁体   中英

How can SWT layout resize (grow/shrink) based on content

I have an explorer in my RCP application to which I added an inner composite with a RowLayout to show various category items. When the width of the explorer is resized I want the category items to wrap to new rows and expand the size of their parent composite.

If I initialize my inner composite with defaults, nothing happens, not even wrapping. If I set it to grab vertical space, it takes half the space, leaving the other half for the tree, which is ugly and not what is wanted. If I set hints, I does wrap but the size of its composite never changes, thus hiding the next rows. I tried adding a resize event listener and resizing my inner composite. That allows me to resize it and show all the rows, but it then covers up and hide part of the tree. I tried to do a setLocation and setSize for the tree itself to move/resize it accordingly but to no avail, it doesn't change.

How can this be made to work. What am I missing. Isn't there a simple way to ask for a layout that will use the minimum required height and no more but adjust if needed?

Thanks for your help.

Here the code:

    innerComposite = new Composite(parent, SWT.NONE);
    innerComposite.addListener(SWT.Resize, listenerComp);
    GridDataFactory.fillDefaults().hint(10, 30).applyTo(innerComposite);
    GridLayoutFactory.fillDefaults().applyTo(innerComposite);

Listener listenerComp = new Listener() {
  public void handleEvent(Event event) {
    Widget widget = event.widget;
    Composite comp = (Composite)widget;
    Composite parent = comp.getParent();
    Point parentSize = parent.getSize();
    Point size = comp.computeSize(parentSize.x, SWT.DEFAULT);
    comp.setSize(size);
  }
}

And here's an image:

在此处输入图片说明

And after resize (noticing that the 1st row of the tree is covered up):

在此处输入图片说明

When you are using Layouts calling setSize (or setBounds ) will mess up the layout that has been setup.

Instead you should call layout(true) (or even layout(true, true) on the Composite which owns both the Tree and your innerComposite .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM