更新父级大小

时间:2014-07-26 23:09:19

标签: java layout swt

我有一个函数可以在添加子项时更新Composite的所有父项的大小,但它不起作用。这是我的代码:

private void increaseSize(Composite child, Composite parent) {
    parent.layout();
    Rectangle childPosition = child.getBounds();
    Rectangle parentPosition = parent.getBounds();
    int parentY = parent.toDisplay(parentPosition.width, parentPosition.height).y;
    int childY = child.toDisplay(childPosition.width, childPosition.height).y;
    if (childY > parentY) {
        int heightDifference = childY - parentY;
        parent.setSize(parentPosition.width, parentPosition.height + heightDifference + 5);
        System.out.println("parent y: " + parentY + ", child y: " + childY);
    } else {
        return;
    }
    parent.layout();
    parent.setVisible(true);
    if (!(parent instanceof Shell)) {
        increaseSize(parent, parent.getParent());
    }

}

如果我删除if - 语句(increaseSize(parent, parent.getParent());)中的行,它对于一个父复合词就可以了。

任何知道什么会使这项工作的人?

更多代码 主:

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

    Shell shell = new Shell (display);
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    shell.setSize(200,100);

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Click here to add Composite");

    GridData selectionData = new GridData(SWT.FILL, SWT.FILL, true, true);
    final CustomComposite selection = new CustomComposite(shell, SWT.BORDER, selectionData);



    button.addSelectionListener(new SelectionListener() {
        @Override
        public void widgetSelected(SelectionEvent selectionEvent) {
            selection.addSelected("I'm added");
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent selectionEvent) {

        }
    });
    shell.open();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

}

CustomComposite:

public class CustomComposite extends Composite{
private GridData layoutData;

public CustomComposite (Composite parent, int style, GridData layoutData) {

    super(parent,style);
    setLayout(new RowLayout());

    setLayoutData(layoutData);
    layoutData.exclude = true;
    this.layoutData = layoutData;

}

public void addSelected(String displayName) {
    SelectedComposite item = new SelectedComposite(this, SWT.BORDER, displayName);
    if (layoutData.exclude == true) {
        layoutData.exclude = false;
        getParent().layout();
    }
    increaseSize(item, this);
    getShell().layout(true, true);
}

private void increaseSize(Composite child, Composite parent) {
    parent.layout();
    Rectangle childPosition = child.getBounds();
    Rectangle parentPosition = parent.getBounds();
    int parentY = parent.toDisplay(parentPosition.width, parentPosition.height).y;
    int childY = child.toDisplay(childPosition.width, childPosition.height).y;
    if (childY > parentY) {
        int heightDifference = childY - parentY;
        parent.setSize(parentPosition.width, parentPosition.height + heightDifference + 5);
    } else {
        return;
    }
    parent.setVisible(true);
    if (!(parent instanceof Shell)) {
        increaseSize(parent, parent.getParent());
    }

}


class SelectedComposite extends Composite{

    private Label label = new Label(this, SWT.NONE);

    public SelectedComposite (Composite parent, int style, String displayName) {
        super(parent, style);
        setLayout(new FillLayout());
        label.setText(displayName);
    }
}

}

0 个答案:

没有答案