11

column = 1のコンポジットがあるとしましょうGridLayout(垂直フロー レイアウトのようなもの)。

このコンポジットにラベル 1、ラベル 2、ラベル 3 を追加したので、それに応じて表示されます。

----------
Label 1  |
Label 2  |
Label 3  |
----------

では、Label 2 の可視性を に設定した場合false、Label 3 が上に移動して Label 2 を置き換えることは可能ですか? ラベル 2 の可視性が に戻されるとtrue、ラベル 3 は下に移動しますか?

4

1 に答える 1

24

非常に単純なソリューションでは、GridData::excludeプロパティを使用できます。例えば、

コード

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class HideLabel 
{
    public static void main(String[] args)
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        shell.setText("Hide Label");

        Label label = new Label(shell, SWT.NONE);
        label.setText("Label 1");

        final Label bHidden = new Label(shell, SWT.NONE);
        bHidden.setText("Label 2");
        GridData data = new GridData();
        data.exclude = false;
        data.horizontalAlignment = SWT.FILL;
        bHidden.setLayoutData(data);

        label = new Label(shell, SWT.NONE);
        label.setText("Label 3");

        Button button = new Button(shell, SWT.CHECK);
        button.setText("hide");
        button.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
                Button b = (Button) e.widget;
                GridData data = (GridData) bHidden.getLayoutData();
                data.exclude = b.getSelection();
                bHidden.setVisible(!data.exclude);
                shell.layout(false);
            }
        });
        shell.setSize(200, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
于 2012-08-30T08:46:28.323 に答える