How to align label from the same starting point in Swt Java - java

I have created 2 labels with the exact same composite and same layoutData. I want my labels to be seen towards the end of the screen and also the starting letter of both the labels should begin from the same point.
Label l1 = new Label(composite, SWT.None);
l1.setLayoutData(new GridData(SWT.END, SWT.CENTER,false, false));
l1.setText("Unmapped");
Label l2 = new Label(composite, SWT.None);
l2.setLayoutData(new GridData(SWT.END, SWT.CENTER,false, false));
l2.setText("Mapped");
Right now, with the above code I do get my output towards the right end of the screen but the starting letters of the both the labels are at 2 different positions and not from the same starting position.
How can I achieve this?

Put the labels inside another composite. This composite is aligned to the end of its parent, and the labels are at the beginning of their parent:
Composite labelComposite = new Composite(composite, SWT.NONE);
labelComposite.setLayout(new GridLayout());
labelComposite.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Label l1 = new Label(labelComposite, SWT.LEAD);
l1.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
l1.setText("Unmapped");
Label l2 = new Label(labelComposite, SWT.LEAD);
l2.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
l2.setText("Mapped");

Related

How to set a composite to the right side of the screen

I have created 2 labels and I wish to move these labels to the right most side of the screen. I have created a new composite for the 2 labels and set the labels with the label composite. But now the label is somewhere towards the center. I have 7 other widgets already in the screen.
Following is the code I used:
Composite blockControls = this.formToolkit.createComposite(this.mainComposite, SWT.NONE);
blockControls.setLayout(GridLayoutFactory.fillDefaults().numColumns(8).create());
blockControls.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
Composite labelComposite = new Composite(blockControls, SWT.NONE);
GridLayout layout = new GridLayout();
labelComposite.setLayout(layout);
GridData layoutData = new GridData(SWT.END, SWT.END, true, true);
labelComposite.setLayoutData(layoutData);
labelComposite.setBackground(GUIHelper.COLOR_WHITE);
Label mappedEleLabel = new Label(labelComposite, SWT.LEAD);
mappedEleLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
mappedEleLabel.setText("hello");
mappedEleLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_YELLOW));
mappedEleLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
Label unmappedEleLabel = new Label(labelComposite, SWT.LEAD);
unmappedEleLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
unmappedEleLabel.setText("world");
unmappedEleLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
unmappedEleLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
You can use FormData but there are many ways.
FormData objects specify how each widget in a FormLayout will be laid out.
But the solution depends on the arrangement of the other 7 widgets.
I recommend this article: Layouts in SWT
Though the article may seem deprecated , Layouts in SWT haven't changed.

Eclipse-Plugin: Separator Line between FieldEditor possible?

When you set up an eclipse plugin preference page, is it possible to add some sort of text or a separator line between the FieldEditors?
#Override
protected void createFieldEditors() {
addField( new SomeFieldEditor(....));
addField( new SomeFieldEditor(....));
}
You can use something like:
Label label = new Label(getFieldEditorParent(), SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 3, 1));
to add a blank like, or
Label label = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1));
to add a horizontal separator.
This assumes you are using the GRID layout. You might have to adjust the '3' in the GridData depending on how many columns the field editor end up using.

How to align two composites in a parent composite without using widthHint and heightHint

I want two composites (one below the other) inside a parent component.
This is how my code looks like:
Composite composite = (Composite) super.createDialogArea(parent);
Composite rowComposite = new Composite(composite, SWT.NONE);
rowComposite.setLayout(new GridLayout(2, false));
GridData gd1 = new GridData(SWT.LEFT, SWT.CENTER, false, false);
gd1.widthHint = 760;
gd1.heightHint = 240;
rowComposite.setLayoutData(gd1);
Composite columnComposite = new Composite(composite, SWT.NONE);
columnComposite .setLayout(new GridLayout(2, false));
GridData gd2 = new GridData(SWT.LEFT, SWT.CENTER, false, false);
gd2.widthHint = 760;
gd2.heightHint = 240;
columnComposite .setLayoutData(gd1);
Here, I'm using widthHint and heightHint which is not recommended as the whole layout will be ruined when the user decides to change the system font or resolution.
How do i achieve the same without using widthHint and heightHint.
Removed the widthHint and heightHint from both composites and updated grid data as:
new GridData(SWT.FILL, SWT.FILL, true, true);
The GridLayout has taken care of width and height.
composite.setLayout(new GridLayout(1, true));
Set layout of parent to GridLayout, which the first parameter is the number of columns and the second is whether or not the columns will have equal width.
I suggest you to try WidowBuilder Pro
This will help you to build HMI rapidly

How to set Horizontal scroll enabled for SWT Scrolled Composite

I have a CTabItem inside a CTabFolder and i want horizontal and vertical scrolling inside my tab item. Below is my code :
composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(4, false));
mainTabFolder = new CTabFolder(composite, SWT.BORDER );
GridData mainTabGD = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
mainTabGD.widthHint = 550;
mainTabGD.heightHint = 350;
mainTabFolder.setLayoutData(mainTabGD);
generalTabItem = new CTabItem(mainTabFolder, SWT.NONE);
generalTabItem.setText("General");
ScrolledComposite sc = new ScrolledComposite(mainTabFolder, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL );
generalTabComposite = new Composite(sc, SWT.NONE);
generalTabComposite.setLayout(new GridLayout(4, false));
// I have created some SWT widgets here
generalTabItem.setControl(sc);
sc.setContent(generalTabComposite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(generalTabComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Above code gives me a vertical scroll, but i'm not able to achieve horizontal scroll.
Please let me know where am i going wrong.
Thanks
You are telling the ScrolledComposite it's minimal size in your last line of code:
sc.setMinSize(generalTabComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
If the composite's size is larger than this value, it will not show scrollbars.

ScrolledComposite parent with GridLayout

I'd like to have a ScrolledComposite which has a parent with GridLayout but the scrollbar doesn't show up, unless I use FillLayout. My problem with FillLayout is that its children takes equal parts of the available space.
In my case there are two widgets, the one on top should take not more than 1/4 of the window and the ScrolledComposite should take the remainder space. However, both of them take half of it.
Is there a way to use a GridLayout with ScrolledComposite or is it possible to modify the behaviour of FillLayout?
Here's my code:
private void initContent() {
//GridLayout shellLayout = new GridLayout();
//shellLayout.numColumns = 1;
//shellLayout.verticalSpacing = 10;
//shell.setLayout(shellLayout);
shell.setLayout(new FillLayout(SWT.VERTICAL));
searchComposite = new SearchComposite(shell, SWT.NONE);
searchComposite.getSearchButton().addListener(SWT.Selection, this);
ScrolledComposite scroll = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
scroll.setLayout(new GridLayout(1, true));
Composite scrollContent = new Composite(scroll, SWT.NONE);
scrollContent.setLayout(new GridLayout(1, true));
for (ChangeDescription description : getChanges(false)) {
ChangesComposite cc = new ChangesComposite(scrollContent, description);
}
scroll.setMinSize(scrollContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scroll.setContent(scrollContent);
scroll.setExpandVertical(true);
scroll.setExpandHorizontal(true);
scroll.setAlwaysShowScrollBars(true);
}
In addition to setLayout(), it is necessary to call setLayoutData(). In the following code example, take a look at how the GridData objects are constructed and passed to each of the two setLayoutData() calls.
private void initContent(Shell shell)
{
// Configure shell
shell.setLayout(new GridLayout());
// Configure standard composite
Composite standardComposite = new Composite(shell, SWT.NONE);
standardComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
// Configure scrolled composite
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
scrolledComposite.setLayout(new GridLayout());
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setAlwaysShowScrollBars(true);
// Add content to scrolled composite
Composite scrolledContent = new Composite(scrolledComposite, SWT.NONE);
scrolledContent.setLayout(new GridLayout());
scrolledComposite.setContent(scrolledContent);
}
NB! This answer is based on Eclipse RAP which might behave differently then regular SWT.
I was struggling with the exact same issue a couple of days ago. I had two ScrolledComposites on the same page and i needed that the left one would not take more space then needed (even if the space would be available).
While trying out different solutions i noticed that the behavior of a ScrolledComposite depends on its LayoutData as follows:
If the layoutData is set to new GridData(SWT.LEFT, SWT.TOP, false, true), then the ScrolledComposite will keep it's intended size regardless of parent Composite size changes.
If the layoutData is set to new GridData(SWT.LEFT, SWT.TOP, true, true), then the ScrolledComposite will shrink/expand according to the size changes of the parent Composite. This also includes expanding to greater width that was desired (meaning that the columns are kept equal).
Based on this behavior i was able to solve the problem by adding a resize listener to the parent Composite that changes the layoutData of the left ScrolledComposite based on the parent Composite size.
This approach is illustrated the following example:
public class LayoutingScrolledComposites extends AbstractEntryPoint {
public void createContents( Composite parent ) {
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
parent.setLayout(new GridLayout(2, false));
ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
Composite c1 = new Composite(sc1, SWT.BORDER);
sc1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
c1.setLayout(new GridLayout(3, false));
sc1.setContent(c1);
Label l1 = new Label (c1, SWT.BORDER);
l1.setText("Some text");
l1 = new Label (c1, SWT.BORDER);
l1.setText("Some text");
l1 = new Label (c1, SWT.BORDER);
l1.setText("Some text");
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
ScrolledComposite sc2 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
sc2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
Composite c2 = new Composite(sc1, SWT.BORDER);
c2.setLayout(new GridLayout(3, false));
sc2.setContent(c2);
Label l2 = new Label (c2, SWT.BORDER);
l2.setText("Some text");
l2 = new Label (c2, SWT.BORDER);
l2.setText("Some text");
l2 = new Label (c2, SWT.BORDER);
l2.setText("Some text");
c2.setSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
parent.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
int sc1_x = sc1.getContent().getSize().x;
int sc2_x = sc2.getContent().getSize().x;
//Enable/Disable grabExcessHorizontalSpace based on whether both sc's would fit in the shell
if (LayoutingScrolledComposites.this.getShell().getSize().x > sc1_x+sc2_x) {
if (((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace) {
//sc1 does not change width in this mode
((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace=false;
}
} else {
if (!((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace) {
//sc1 changes width in this mode
((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace=true;
}
}
parent.layout(); //Needed so that the layout change would take effect during the same event
}
});
}
}
However this approach does seem to me a bit too "hackish" solution. Therefore i would love to see a better approach.
I think what you're missing here is to define the GridData for the children.
A layout controls the position and size of children. And every layout class has a corresponding layout data class which allows to configure each specific children within the layout, if they fill up the whole space, how many cells they take, etc.
I guess your grid layout could have 4 rows, with the widget on top taking just one cell and the other child taking the rest (3). This is achieved through the GridData.verticalSpan property.
Take a look at Understanding Layouts in SWT and try the different layout data properties to see what they do.

Categories

Resources