Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT components relayout after visibility set to false

Tags:

java

layout

swt

Lets say if I have a GridLayout composite with column = 1. (Something like a vertical flow layout)

I have added Label 1, Label 2, Label 3 to this composite, and they will appear accordingly.

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

So is it possible that if I set the visibility of Label 2 to be false, can Label 3 move up to replace Label 2? And if Label 2 visibility is set back to true, Label 3 will move down?

like image 1000
humansg Avatar asked Aug 30 '12 03:08

humansg


People also ask

What is SWT composite?

Composite is an ancestor of every SWT class to which you can add widgets, including Shell . For that reason, you can think of a Composite as being simply the working area of a Shell , without the styles associated with the aspects of Shell that make windows—the titlebar, control menu, etc.

What is SWT in programming?

SWT is an open source widget toolkit for Java designed to provide efficient, portable access to the user-interface facilities of the operating systems on which it is implemented.

What is an SWT application?

The Standard Widget Toolkit (SWT) is a Java based user interface library for developing desktop application. SWT supports Windows, Linux and Mac OS X. It provides lots of standard widgets, e.g., buttons and text fields as well as the option to create custom widgets.


1 Answers

A very simple solution could use GridData::exclude property. For example,

Code

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();
    }
}
like image 105
Favonius Avatar answered Sep 18 '22 15:09

Favonius