Mandy
Mandy

Reputation: 1177

How to add 2 different composite having different layout on main composite?

I want to show view which contains 2 Tree Viewers and one Table Viewer.
It will look as follow,


TreeViewer1 | TreeViewer2

-------------TableViewer------------


(Sorry as I can't upload the image from my machine due to some restrictions, but the above controls must fill the entire area of the view)

For this I had created one mainComposite, which will hold all the controls and which is having RowLayout with SWT.VERTICAL style.
After that I had created top composite which is going to hold TreeViewer1 and TreeViewer2, and which is having Grid layout with 2 columns.(Where each column will contain one TreeViewer resp.)
After that I had created bottom composite which is going to hold TableViewer, and which is again having grid layout with 1 column.

mainComposite holds top and bottom composite. The top and bottom composite needs to share mainComposites height equally and both composites needs to acquire entire width of mainComposite.

When I run the program, my controls are coming in order as I want.But they are not acquiring the entire width of the composite.( i.e. they are coming in left corner ).
I tried using different type of layouts but no help.

I tried with the post http://www.programcreek.com/2012/03/eclipse-rcp-tutorial-5-how-to-layout-your-view-gridlayout-example/
but didn't work for me since I am having table viewer and not Text.

Any help is appreciated.

Regards,
Mandar

Upvotes: 2

Views: 2367

Answers (2)

stracka
stracka

Reputation: 1023

You can get the behavior that I think you're looking for (both trees as well as the table using all available space) by using a bunch of GridLayouts with alignments set to SWT.FILL and both grabExcess*Space parameters set to true.

Try this:

@Override
public void createPartControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout gl_container = new GridLayout(1, false);
    gl_container.horizontalSpacing = 15;
    container.setLayout(gl_container);

    Composite mainComposite = new Composite(container, SWT.NONE);
    mainComposite.setLayout(new GridLayout(1, false));
    mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Composite treesComposite = new Composite(mainComposite, SWT.NONE);
    treesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    treesComposite.setLayout(new GridLayout(2, false));

    TreeViewer leftTreeViewer = new TreeViewer(treesComposite, SWT.BORDER);
    Tree leftTree = leftTreeViewer.getTree();
    leftTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TreeViewer rightTreeViewer = new TreeViewer(treesComposite, SWT.BORDER);
    Tree rightTree = rightTreeViewer.getTree();
    rightTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TableViewer bottomTableViewer = new TableViewer(mainComposite, SWT.BORDER | SWT.FULL_SELECTION);
    bottomTable = bottomTableViewer.getTable();
    bottomTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
}

Alternatively, you could try using FormLayouts.

Here I specify the locations of things using the "numerator/offset" approach. Where you see numbers like 0/50/100, those are essentially percentages of the available space. The smaller numbers like 5/-5 are offsets, in pixels, from the positions described by those percentages; they provide a small margin between components.

@Override
public void createPartControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout gl_container = new GridLayout(1, false);
    gl_container.horizontalSpacing = 15;
    container.setLayout(gl_container);

    Composite mainComposite = new Composite(container, SWT.NONE);
    mainComposite.setLayout(new FormLayout());
    mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Composite treesComposite = new Composite(mainComposite, SWT.NONE);
    FormData fd_treesComposite = new FormData();
    fd_treesComposite.bottom = new FormAttachment(50);
    fd_treesComposite.right = new FormAttachment(100);
    fd_treesComposite.top = new FormAttachment(0);
    fd_treesComposite.left = new FormAttachment(0);
    treesComposite.setLayoutData(fd_treesComposite);
    treesComposite.setLayout(new FormLayout());

    TreeViewer leftTreeViewer = new TreeViewer(treesComposite, SWT.BORDER);
    Tree leftTree = leftTreeViewer.getTree();
    FormData fd_leftTree = new FormData();
    fd_leftTree.bottom = new FormAttachment(100);
    fd_leftTree.right = new FormAttachment(50, -2);
    fd_leftTree.top = new FormAttachment(0, 5);
    fd_leftTree.left = new FormAttachment(0, 5);
    leftTree.setLayoutData(fd_leftTree);

    TreeViewer rightTreeViewer = new TreeViewer(treesComposite, SWT.BORDER);
    Tree rightTree = rightTreeViewer.getTree();
    FormData fd_rightTree = new FormData();
    fd_rightTree.bottom = new FormAttachment(100);
    fd_rightTree.right = new FormAttachment(100, -5);
    fd_rightTree.top = new FormAttachment(0, 5);
    fd_rightTree.left = new FormAttachment(50, 3);
    rightTree.setLayoutData(fd_rightTree);

    TableViewer bottomTableViewer = new TableViewer(mainComposite, SWT.BORDER | SWT.FULL_SELECTION);
    bottomTable = bottomTableViewer.getTable();
    FormData fd_bottomTable = new FormData();
    fd_bottomTable.bottom = new FormAttachment(100, -5);
    fd_bottomTable.right = new FormAttachment(100, -5);
    fd_bottomTable.top = new FormAttachment(50, 5);
    fd_bottomTable.left = new FormAttachment(0, 5);
    bottomTable.setLayoutData(fd_bottomTable);
}

Upvotes: 0

aphex
aphex

Reputation: 3412

Use in all composites FillLayout. Here is an small example how to use it. Important is to set SWT.VERTICAL/SWT.HORIZONTAL.

Upvotes: 0

Related Questions