Unable to set size for a scrolled composite SWT - swt

I have a main composite and within it two scrolled composites. The first scrolled composite does not have anything within it as of now. The second scrolled composite has a list. The problem is since that list is large, the second scrolled composite is occupying majority of the main composite area and leaves just a small square portion for the first scrolled composite. I want the two scrolled composite to be of at least equal sizes or one bigger than the other. I am not able to achieve this using the setSize() methods for the composites.
Code:
/** The main composite */
Composite leftComposite = new Composite(sashFormLeftRight, SWT.BORDER);
GridData leftCompositeGD = new GridData(SWT.FILL, GridData.FILL, true, false);
leftComposite.setLayoutData(leftCompositeGD);
leftComposite.setLayout(new GridLayout(1, false));
/** The first scrolled composite. Its UI elements will be filled at a later stage based on user input */
ScrolledComposite scrolledComposite = new ScrolledComposite(leftComposite, SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Composite composite = new Composite(scrolledComposite, SWT.BORDER);
composite.setLayout(new GridLayout(1, true));
composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
scrolledComposite.setContent(composite);
composite.layout(true, true);
scrolledComposite.layout(true, true);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
/** The second scrolled composite. It has an inner composite which has a list of string names */
ScrolledComposite scrolledParamComposite = new ScrolledComposite(leftComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
GridData scrolledParamCompositeGD = new GridData(SWT.FILL, GridData.FILL, true, true);
scrolledParamComposite.setLayoutData(scrolledParamCompositeGD);
scrolledParamComposite.setExpandHorizontal(true);
scrolledParamComposite.setExpandVertical(true);
Composite innerComp = new Composite(scrolledParamComposite, SWT.BORDER );
innerComp.setLayout(new GridLayout(1, true));
scrolledParamComposite.setContent(innerComp);
/** List which fills the second scrolled composite */
List parametersListForEnv = new List(innerComp, SWT.BORDER);
ParameterName[] namesList = Types.ParameterName.values();
for (int i = 0; i < namesList.length; i++) {
parametersListForEnv.add(namesList[i].name());
}
innerComp.layout(true, true);
scrolledParamComposite.layout(true, true);
scrolledParamComposite.setMinSize(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Kindly help.
Thanks in advance.

Have you tried:
GridData compositeData = new GridData(GridData.FILL, GridData.FILL, true, true);
compositeData.heightHint = // your preferred height or the height of the other composite ;
compositeData.minimumHeight = // your preferred minimum height or the height of the other composite ;
and then
composite.setLayoutData(compositeData);
I had a similar problem and this worked for me.

Related

Layout problems with customized FieldEditor (Eclipse Preference Page)

I try to create my own FieldEditor (as I have to fill combobox values dynamically). So my class extends 'FieldEditor'. My preference page needs 3 of these fields which then look like this (2nd,3rd and 4th field-editors; the 'select kernel' ones).
Obviously something goes wrong with the layout. All fields should look like the 3rd field - using the full space.
#Override
protected void adjustForNumColumns(int numColumns) {
((GridData) c_top.getLayoutData()).horizontalSpan = numColumns;
}
#Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
/* Layout comments:
*
* component are sequentially filled into numColumns
* by default each component will use 1 column
* GridData can be set to use more that one columns
*/
GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
gd.horizontalSpan = numColumns;
c_top = parent;
c_top.setLayoutData(gd);
c_group = new Composite(c_top, SWT.BORDER);
GridLayout newgd = new GridLayout(2, false);
c_group.setLayout(newgd);
c_group.setLayoutData(gd);
// kernel spec combo
Label comboLabel = new Label(c_group, SWT.NONE);
comboLabel.setText("Select kernel");
gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
gd.horizontalSpan = numColumns - 1;
comboLabel.setLayoutData(gd);
c_kernelCombo = new Combo(c_group, SWT.READ_ONLY);
gd = new GridData(SWT.FILL, SWT.TOP, true, false);
//gd.horizontalSpan = 1;
c_kernelCombo.setLayoutData(gd);
}
I even tried a simpler layout without using a group but then all my field-editors only used 2 cells of the grid (which looks a bit funny with 3 columns given by the other field editors.
I have no idea how to fix it . Can anybody please help?
You are setting the same instance of GridData (gd) on two controls - this is not allowed. You must create a new GridData for each control.
In any case you should not be setting the layout data on the parent Composite - that doesn't belong to your code.
I got a solution but I don't know whether it's correct:
1) I made the mistake to create all FieldEditors of that page with the same parent, though the docs say to retrieve a new parent for each field editor with getFieldEditorParent()
2) I guess, I misunderstood the meaning of adjustForNumColumns(int numColumns). I asume, it should adapt the horizontalSpan of the controls which are affected by a change of the number of columns. Now my code looks like this:
#Override
protected void adjustForNumColumns(int numColumns) {
((GridData) c_kernelCombo.getLayoutData()).horizontalSpan = numColumns-1;
}
#Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
// kernel spec combo
Label comboLabel = new Label(parent, SWT.NONE);
comboLabel.setText("Select kernel");
GridData gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
gd.horizontalSpan = 1;
comboLabel.setLayoutData(gd);
c_kernelCombo = new Combo(parent, SWT.READ_ONLY);
gd = new GridData(SWT.FILL, SWT.TOP, true, false);
gd.horizontalSpan = numColumns - 1;
c_kernelCombo.setLayoutData(gd);
}

Composite with label aligned left and buttons right

I work on an Eclipse plug-in and at a moment in time a pop-up is shown. Inside the pop-up dialog box, I want to create an area where I have a label on the left and two buttons alligned right.
public void createBottom(Composite parent) {
Composite composite = new Composite(parent, SWT.FILL | SWT.WRAP | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
addLabel(composite);
addButton1(composite);
addButton2(composite);
}
Currently the result looks like this:
While I'm expecting something more like this:
How can I possibly align the Label on the left and the two buttons on the right?
First SWT.FILL and SWT.WRAP are not valid styles for Composite. The Javadoc for the control specifies which styles you can use.
Use something like:
Composite composite = new Composite(parent, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false);
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(3, false));
Label label = new Label(composite, SWT.BEGINNING);
label.setText("Test");
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button but1 = new Button(composite, SWT.PUSH);
but1.setText("OK");
but1.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Button but2 = new Button(composite, SWT.PUSH);
but2.setText("Close");
but2.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
The layout data for the label grabs the extra space in the composite, and the buttons have end alignment.

Eclipse e4: creating a 3 part window trim without resizing text in tool control

I created a Window Trim - Top
now I add 3 Tool Control
first only should contain a SWT-Text and not resize ever...
however, when I type some text and resize my window, it automatically resizes the SWT-Text to fit the text, which it should not.
So how can I give that Tool Control, or the Composite, or the Text the right Size and tell it, NOT to resize!?
public class TrimBarSearch {
#Inject
ISearchService searchService;
private Text txtSearch;
private Composite composite;
#Inject
public TrimBarSearch() {
}
#PostConstruct
public void createGui(final Composite parent) {
parent.setLayoutData(new GridLayout(3, false));
composite = new Composite(parent, SWT.NONE);
Point xy = new Point(300, 15);
Point sizeComposite = new Point(310, 25);
composite.setLayout(new GridLayout(1, false));
composite.setSize(sizeComposite);
txtSearch = new Text(composite, SWT.FILL);
txtSearch.setSize(xy);
txtSearch.setText("");
// TODO fix resizing-problem
parent.getShell().addListener(SWT.Resize, e -> {
//maybe here?!
});}
Never try and mix Layouts with setSize - it does not work, the layout will override your size.
Instead you can specify a width hint for the text in the GridData for the text. Instead of:
txtSearch.setSize(xy);
use:
GridData data = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
data.widthHint = 300;
txtSize.setLayoutData(data);

Avoid Tree Resize after Section expand

I've got a question I couldn't resolve myself for quite a while now.
I have a RCP ViewPage containing two sections. The sections are inside a SashForm so that the user is able to resize the expanded sections. In the bottom section there is a Tree which is empty after initialization. Through user interaction (i.e. removing a filter) the tree gets filled and has a lot of data in it. If the user now collapses the bottom view and expands it again the tree gets resized which causes ScrollBars in my form. What I want is scrollbars in the tree view.
Here is how the view is build:
- ScrolledForm
- Form Body
- Sash
- Section 1
- Composite
- Some View
- Section 2
- Composite
- Tree
I hope you understand what I'm trying to achieve.
UPDATE: Here is some source code to play with. It uses a Table instead of a tree but produces the same issue.
public class MyPersonPageEditor extends FormPage {
public static final String ID = "some.ID";
TableViewer tableViewer;
public MyPersonPageEditor(FormEditor editor) {
super(editor, ID, "Some Title");
}
#Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm form = managedForm.getForm();
Composite formBody = form.getBody();
formBody.setLayout(new GridLayout());
form.setText("Some Title");
toolkit.decorateFormHeading(form.getForm());
SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
sfForm.setLayout(new GridLayout());
sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
topSection.setText("Section 1 Title");
Composite topSectionComposite = toolkit.createComposite(topSection);
topSectionComposite.setLayout(new GridLayout());
toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
btn.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0 ; i < 10 ; i++) {
tableViewer.add("Element " + i);
}
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
topSection.setClient(topSectionComposite);
Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
bottomSection.setText("Section 2 Title");
Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
bottomSectionComposite.setLayout(new GridLayout());
bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
bottomSection.setClient(bottomSectionComposite);
Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION |
SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setHeaderVisible(true);
tableViewer = new TableViewer(table);
tableViewer.add("New Element");
new TableColumn(table, SWT.LEFT).setText("Spalte 1");
TableLayout layoutDefault = new TableLayout();
layoutDefault.addColumnData(new ColumnWeightData(1));
table.setLayout(layoutDefault);
form.reflow(true);
}
}
If you click the button after start the table looks like the left picture. After you collapse and expand the second section it looks like the right one.
Here is the code that works: you just have to tweak the size of the Tree/Table and make sure it will not span across the vertical space:
public void createPartControl(Composite parent) {
FormToolkit toolkit = new FormToolkit(parent.getDisplay());
final ScrolledForm form = toolkit.createScrolledForm(parent);
Composite formBody = form.getBody();
formBody.setLayout(new GridLayout());
form.setText("Some Title");
toolkit.decorateFormHeading(form.getForm());
SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
sfForm.setLayout(new GridLayout());
sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
//top section
Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
topSection.setLayout(new GridLayout());
topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
topSection.setText("Section 1 Title");
Composite topSectionComposite = toolkit.createComposite(topSection);
topSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
topSectionComposite.setLayout(new TableWrapLayout());
toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
btn.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0 ; i < 10 ; i++) {
tableViewer.add("Element " + i);
}
form.reflow(true);
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
topSection.setClient(topSectionComposite);
//bottom section
Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
bottomSection.setLayout(new GridLayout());
bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
bottomSection.setText("Section 2 Title");
Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
bottomSectionComposite.setLayout(new TableWrapLayout());
Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION |
SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);
TableWrapData ttd222 = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL_GRAB);
ttd222.maxHeight =200;
table.setLayoutData(ttd222);
table.setHeaderVisible(true);
tableViewer = new TableViewer(table);
tableViewer.add("New Element");
new TableColumn(table, SWT.LEFT).setText("Spalte 1");
TableLayout layoutDefault = new TableLayout();
layoutDefault.addColumnData(new ColumnWeightData(1));
table.setLayout(layoutDefault);
bottomSection.setClient(bottomSectionComposite);
form.reflow(true);
}
-make sure the Tree has the SWT.H_SCROLL style (if I remember correctly), that is has a minimum size (for example with GridLayout and GridData set the minHeight to X or with TableWrapLayout and TableData heightHint to X)
-if you don't manage to make it work just tell me and I'll try to make the code; also, a picture with the layout would be great

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
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);
}
Use in all composites FillLayout. Here is an small example how to use it. Important is to set SWT.VERTICAL/SWT.HORIZONTAL.