改变SWT综合体儿童的顺序

时间:2010-05-06 11:31:39

标签: java swt

在我的情况下,我有两个SashForm的孩子,但问题适用于所有Composite

class MainWindow {
    Sashform sashform;
    Tree child1 = null;
    Table child2 = null;

    MainWindow(Shell shell) {
        sashform = new SashForm(shell, SWT.NONE);
    }

    // Not called from constructor because it needs data not available at that time
    void CreateFirstChild() {  
        ...
        Tree child1 = new Tree(sashform, SWT.NONE);
    }

    void CreateSecondChild() {
        ...
        Table child2 = new Table(sashform, SWT.NONE);
    }    
}

我事先并不知道调用这些方法的顺序。如何确保child1位于左侧,child2位于右侧?或者,有没有办法在创建之后更改sashform 的孩子的顺序?

目前我最好的想法就是放置这样的占位符:

class MainWindow {
    Sashform sashform;
    private Composite placeholder1;
    private Composite placeholder2;
    Tree child1 = null;
    Table child2 = null;

    MainWindow(Shell shell) {
        sashform = new SashForm(shell, SWT.NONE);
        placeholder1 = new Composite(sashform, SWT.NONE);
        placeholder2 = new Composite(sashform, SWT.NONE);
    }

    void CreateFirstChild() {  
        ...
        Tree child1 = new Tree(placeholder1, SWT.NONE);
    }

    void CreateSecondChild() {
        ...
        Table child2 = new Table(placeholder2, SWT.NONE);
    }    
}

1 个答案:

答案 0 :(得分:14)

创建child1时,检查child2是否已经实例化。如果是,则表示child1在右侧,因为它已在稍后创建,因此您必须这样做:

child1.moveAbove( child2 );

希望它有所帮助。