Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there layout like cardLayout in SWT\RCP

Tags:

java

layout

swt

I want Layout in SWT which acts like cardLayout of Swing. My Main requirement is like I have a checkBox and have 2 SWT groups.

And based on checkbox selection and deselection need to show the SWT groups respectively in the same position.

Precisely only one group must be visible at a time based in checkbox state. How to achieve the same.

like image 623
Abhishek Choudhary Avatar asked Jan 08 '14 14:01

Abhishek Choudhary


1 Answers

You can use a StackLayout to achieve what you want:

private static boolean buttonOnTop = true;

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");

    shell.setLayout(new FillLayout());

    Button switchButton = new Button(shell, SWT.NONE);
    switchButton.setText("Switch");

    final StackLayout layout = new StackLayout();

    final Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(layout);

    final Button button = new Button(content, SWT.PUSH);
    button.setText("Button");

    final Label label = new Label(content, SWT.NONE);
    label.setText("Label");

    layout.topControl = button;

    switchButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            layout.topControl = (buttonOnTop) ? label : button;
            content.layout();

            buttonOnTop = !buttonOnTop;
        }
    });

    shell.pack();
    shell.open();

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

By setting StackLayout#topControl you can "move" your Control to the top.

like image 105
Baz Avatar answered Oct 03 '22 05:10

Baz