Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder GridLayout SWT

Tags:

java

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();

like image 631
Johnny000 Avatar asked Mar 22 '23 23:03

Johnny000


1 Answers

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

like image 146
Baz Avatar answered Mar 24 '23 12:03

Baz