Question

From API document of GridLayout: "Widgets are laid out in columns from left to right,...", I'm wondering if there is someway to lay out the widgets from right to left? Or, when I just have one column which applies GridLayout, can I lay out widgets from bottom to top? (Sorry for my poor English!)

Was it helpful?

Solution

The magic word here is SWT.RIGHT_TO_LEFT. However, it is not applied to the Layout, but to the Composite containing your widgets.

Here is some example code:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, true));
    shell.setText("ExpandBar Example");

    Composite ltr = new Composite(shell, SWT.LEFT_TO_RIGHT);
    ltr.setLayout(new GridLayout(3, true));
    ltr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    Composite rtl = new Composite(shell, SWT.RIGHT_TO_LEFT);
    rtl.setLayout(new GridLayout(3, true));
    rtl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int i = 0; i < 10; i++)
    {
        new Button(ltr, SWT.PUSH).setText("Button " + i);
        new Button(rtl, SWT.PUSH).setText("Button " + i);
    }

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

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

And this is what it looks like:

enter image description here


To get both right-to-left and bottom-to-top, you'll just have to use moveAbove/moveBelow with null as the parameter:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, true));
    shell.setText("StackOverflow");

    Composite ltr = new Composite(shell, SWT.LEFT_TO_RIGHT);
    ltr.setLayout(new GridLayout(3, true));
    ltr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    Composite rtl = new Composite(shell, SWT.LEFT_TO_RIGHT);
    rtl.setLayout(new GridLayout(3, true));
    rtl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    int nrOfItems = 10;
    int nrOfColumns = ((GridLayout) rtl.getLayout()).numColumns;
    int fillerItems = nrOfColumns - nrOfItems % nrOfColumns;

    for (int i = 0; i < nrOfItems; i++)
    {
        new Button(ltr, SWT.PUSH).setText("Button " + i);
        new Button(rtl, SWT.PUSH).setText("Button " + i);
    }

    for(int i = 0; i < fillerItems; i++)
        new Label(rtl, SWT.NONE);

    Control[] children = rtl.getChildren();
    for (int i = 0; i < children.length; i++)
    {
        children[i].moveAbove(null);
    }

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

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

Looks like this:

enter image description here

OTHER TIPS

By referring to SWT Snippets , I found a way to lay out widgets from bottom to top vertically(in the case of single column), just use the moveAbove() method of Control(in this case buttons are generated, Button is a subclass of Control).

Here is an example:

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

    for(int i = 0; i < 10; i++)
    {
        Button btn = new Button(shell, SWT.PUSH);
        btn.setText("Button " + i);
        btn.moveAbove(null); 
    }

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

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

can't post a pic to show the result, for I'm new here, not enough reputation, sorry.

Depending on how to data you want to add is represented, you could perhaps just reverse your loop when creating the components before adding them to the panel? Assuming that you have an array of data, instead of going for (i = 0; i < n; i++), you can just for (i = n-1; i >= 0; i--) which will create a "reverse" effect?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top