简体   繁体   中英

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), eg

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

and

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

where the user can switch between both, eg, 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();
    }
}

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