Johnny000
Johnny000

Reputation: 2104

Reorder GridLayout SWT

I Have a GridLayout filled with Composites in a random order. Now I'm sorting the Composites that are filling the GridLayout in a List/Collection and want to order them like the sort result from my List/Collection. I tried to do it by allocating them again to their parent so they are in the right order, but for some reason nothing happened. Then I tried to cache them in a Composite you don't see and then bring them back to the parent with the same method as in the first attempt. No change at all. Anyone has a pointer? I'm ordering by date, just in case /so want to know.

Thats how my grid looks like, now I want to order them like in my arrayList();

Upvotes: 4

Views: 1245

Answers (2)

Baz
Baz

Reputation: 36894

The methods you are looking for are Control#moveAbove(Control control) and Control#moveBelow(Control control) to reorder the items:

private static List<Label>  labels  = new ArrayList<Label>();
private static List<Color>  colors  = new ArrayList<Color>();

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new RowLayout(SWT.VERTICAL));

    colors.add(display.getSystemColor(SWT.COLOR_BLUE));
    colors.add(display.getSystemColor(SWT.COLOR_CYAN));
    colors.add(display.getSystemColor(SWT.COLOR_GREEN));
    colors.add(display.getSystemColor(SWT.COLOR_YELLOW));
    colors.add(display.getSystemColor(SWT.COLOR_RED));

    for (int i = 0; i < 5; i++)
    {
        Label label = new Label(shell, SWT.BORDER);
        label.setText("Button " + i);
        label.setBackground(colors.get(i));

        labels.add(label);
    }

    Button sortButton = new Button(shell, SWT.TOGGLE);
    sortButton.setText("Sort");

    sortButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Button source = (Button) e.widget;

            final boolean asc = source.getSelection();

            Label oldFirst = labels.get(0);

            Collections.sort(labels, new Comparator<Label>()
            {
                @Override
                public int compare(Label o1, Label o2)
                {
                    int result = o1.getText().compareTo(o2.getText());

                    if (asc)
                        result = -result;

                    return result;
                }
            });

            Label label = labels.get(0);
            label.moveAbove(oldFirst);

            for (int i = 1; i < labels.size(); i++)
            {
                labels.get(i).moveBelow(labels.get(i - 1));
            }
            shell.layout();
        }
    });

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

After starting:

enter image description here

After pressing the button:

enter image description here

Upvotes: 5

Johnny000
Johnny000

Reputation: 2104

I found the solution. You have to call child.moveAbove(otherChild) or .moveBelow() When you are done with the reordering just call on the parent Composite parent.layout()

Upvotes: 2

Related Questions