Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing order of children of an SWT Composite

Tags:

java

swt

In my case I have two children of a SashForm, but the question applies to all Composites.

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

I don't know in advance in what order these methods will be called. How can I make sure that child1 is placed on the left, and child2 on the right? Alternately, is there a way to change their order as children of sashform after they are created?

Currently my best idea is to put in placeholders like this:

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);
    }    
}
like image 722
Alexey Romanov Avatar asked May 06 '10 11:05

Alexey Romanov


1 Answers

When you create child1, check if child2 has already been instantiated. If it is, it means child1 is on the right, because it has been created later, so you have to do this:

child1.moveAbove( child2 );

Hope it helps.

like image 57
Mario Marinato Avatar answered Oct 27 '22 15:10

Mario Marinato