Add scrollbar to JFACE Wizard - java

Problem
I have a Composite in my wizardPage and I want to add scrollbars to it if the Composite is bigger than the wizard window, but no method I tried so far worked. Anyone an idea how to add the scrollbars?
I want to add them to a Composite to which I add text;
compositeInfo = new Composite(container, SWT.BORDER);
What I tried
I tried creating a ScrollableComposite without succes, when I use the ScrollableComponent, the text doesnt get added to the Composite.
compositeInfo = new ScrolledComposite(container, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
compositeInfo.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WHITE));
GridLayout gl = new GridLayout(1, false);
gl.numColumns = 1;
compositeInfo.setLayout(gl);
GridData gd_composite_2 = new GridData(SWT.CENTER, SWT.CENTER, false,
false, 2, 1);
gd_composite_2.widthHint = 450;
compositeInfo.setLayoutData(gd_composite_2);
add some text
c = compositeInfo;
Label lblGD = new Label(c, SWT.NONE);
GridData gd_lblG = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
1);
gd_lblG.widthHint = 450;
lblGD.setLayoutData(gd_lblG);
lblGD.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
lblGD.setFont(SWTResourceManager.getFont("Tahoma", 10, SWT.BOLD));
lblGD.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
lblGD.setAlignment(SWT.LEFT);
lblGD.setText(t);
((ScrolledComposite) c).setContent(lblGD);

See JavaDoc of ScrolledComposite. You need to call either compositeInfo.setSize or compositeInfo.setMinSize.

Related

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.

Java learn using SWT gridlayout and griddata

I understand and can use FormLayout, FormData, FormAttachment but I can't understand how GridLayout, GridData is working. I want to learn using GridLayout and GridData because it's more like a table, it has a structure and doesn't depend on other widgets.
I was working as a web developer (front-end, back-end) and I got lost in Java "Grid" structure. How am I supposed to align, move widgets within a cell (horizontal/vertical Aling, hor./vert. Indent)? Like in HTML/CSS: margin, padding, etc. Ex: move a block from left by 100px. (margin-left: 100px), but in Java?
When I was working as a web developer, I created a page (here in Java it's view), I know how to organize parents and blocks. Can I compare a Composite to a div, like a block element like in HTML/CSS ?
I need to create the following app:
Am I need to use 4 composites?
The following article should shed some light on GridLayout for you:
Understanding Layouts in SWT
To achieve something like the form you have there, you would need something like this:
shell.setLayout(new GridLayout(3, false));
Label title = new Label(shell, SWT.NONE);
title.setText("My first text editor");
GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
title.setLayoutData(data);
Label select = new Label(shell, SWT.NONE);
select.setText("Select a file:");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
select.setLayoutData(data);
Text text = new Text(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
text.setLayoutData(data);
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse...");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
button.setLayoutData(data);
List result = new List(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.horizontalSpan = 3;
result.setLayoutData(data);
The GridData us used to define the behavior of the component within the layout. You can define vertical/horizontal alignment, margins and so on. horizontalSpan is used to tell the layout how many columns the widget will cover.

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