开发者

How to change parent of components with SWT?

My window should allow two different kind of layouts (it's a simple example to better illustrate it), e.g.

+-------------+----开发者_JAVA技巧---------+-------------+
| Component 1 | Component 2 | Component 3 |
|             |             |             |
|             |             |             |
|             |             |             |
|             |             |             |
+-------------+-------------+-------------+

and

+-------------+---------------------------+
| Component 1 | Component 2               |
|             |                           |
|             +---------------------------+
|             | Component 3               |
|             |                           |
+-------------+---------------------------+

where the user can switch between both, e.g., using a menu item.

With SWT you need to provide the parent when creating a component. But we will need to (1) reuse the component and (2) place them in a different parent (similar to docking frameworks). How is this possible with SWT?


You can simply do this by changing a component's parent.

setParent() changes the control's parent, if the underlying operating system supports it. Then you can layout() the composite so the changes appear.

Say you have three controls:

  • A composite c1 containing vertical controls
  • A composite c2 containing horizontal controls
  • A label lbl
  • A button btn

Here's the code:

public class ControlSwitcher {
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        GridLayout gl = new GridLayout();
        gl.marginWidth = gl.marginHeight = 20;
        shell.setLayout(gl);

        final Composite c1 = new Composite(shell, SWT.NONE);
        c1.setBackground(new Color(display, 255, 160, 160));
        RowLayout layout = new RowLayout(SWT.VERTICAL);
        c1.setLayout(layout);

        final Composite c2 = new Composite(c1, SWT.NONE);
        c2.setBackground(new Color(display, 160, 255, 160));
        c2.setLayout(new RowLayout());

        final Label lbl = new Label(c2, SWT.NORMAL);
        lbl.setText("Hello world");

        final Button btn = new Button(c2, SWT.PUSH);
        btn.setText("Switch");
        btn.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                Composite target;
                if (btn.getParent().equals(c2)) {
                    target = c1;
                } else {
                    target = c2;
                }
                boolean success = btn.setParent(target);
                if (success) {
                    target.pack();
                    shell.pack();
                } else {
                    throw new RuntimeException("Not supported by this platform");
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
            }
        });

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜