Change the order of element in RowLayout SWT Java

别说谁变了你拦得住时间么 提交于 2019-12-23 09:32:19

问题


Is there a way to change the order of the elements which are created in Row layout , I want to display it in the elements as first in first displayed. For example if I create element1 ,then element2 element3, element4

I want to see the layout as element4 element3 element2 element1

that mean the elements that last to be created will be the first element which will be displayed in the shell.

Is there easy way to work with the row layout and do it.

I want to change the following example to displayed Button99 Button98 Button97 Button96 Button95 Button94……………………………….

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestExample
{
    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        RowLayout rowLayout = new RowLayout();

        shell.setLayout(rowLayout);

        for (int i=0;i<100;i++)
        {
            Button b1 = new Button(shell, SWT.PUSH);
            b1.setText("Button"+i);

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

Thanks in Advance.


回答1:


FillLayout, RowLayout and GridLayout use the z-order of the controls in order to determine the ordering of the controls. (Since those three layouts don't allow controls to visually overlap each other, the z-order would be otherwise ignored.)

The default z-order is based on creation - thus controls default to the order that you added them to their parent.

You can change the z-order (and thus, change the order that widgets are painted) using the Control.moveAbove() and Control.moveBelow() methods.




回答2:


There seems to be no property to set so that elements of RowLayout will be placed in reverse order. So you can hold the elements in an array reversed and then add them all in loop or for this specific example, just change the start and end condition of the for loop so that it'll be like Button99 Button98 ... :D




回答3:


If you use one of the swt's layout, then:

already: item01, item02, item03 insert item04 before item02: 1. create item04 2. item04.moveAbove(item02)

Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(640, 480);

shell.setLayout(new FillLayout());

final Composite itemComposite = new Composite(shell, SWT.NONE);
itemComposite.setLayout(new RowLayout());
Label item01 = new Label(itemComposite, SWT.NONE);
item01.setText("item01");
final Label item02 = new Label(itemComposite, SWT.NONE);
item02.setText("item02");
Label item03 = new Label(itemComposite, SWT.NONE);
item03.setText("item03");

Composite buttonComposite = new Composite(shell, SWT.NONE);
buttonComposite.setLayout(new GridLayout());
Button insertItem = new Button(buttonComposite, SWT.NONE);
insertItem.setText("Insert");
insertItem.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event arg0) {
    Label item04 = new Label(itemComposite, SWT.NONE);
    item04.setText("item04");
    item04.moveAbove(item02);
    itemComposite.layout();
}
});

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


来源:https://stackoverflow.com/questions/9250946/change-the-order-of-element-in-rowlayout-swt-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!