I am trying to set the height of ToolBar to match other items in the grid layout. Here is the code I am using:
public TestSwt() {
Display display = new Display();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setSize(800, 800);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
shell.setLayout(gridLayout);
Composite composite = new Composite(shell, SWT.NONE);
composite.setSize(100, 100);
GridLayout childGridlayout = new GridLayout();
childGridlayout.numColumns = 10;
composite.setLayout(childGridlayout);
composite.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
final Link link = new Link(composite, SWT.NULL);
link.setText("Link1");
Link link1 = new Link(composite, SWT.NULL);
link1.setText("Link2");
ToolBar toolBar = new ToolBar(composite, SWT.BORDER | SWT.VERTICAL);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("toolbar");
// Trying to resize toolbar
Point size = toolBar.computeSize(SWT.DEFAULT, link.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
toolBar.setSize(size);
toolBar.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
The output I get is:
The output I want is:
I am sure I am missing something very basic. Any help will be really appreciated.
Edit:
As Andrew suggested I set the GridData as LayoutData. It did resize the toolBar but the text is not visible.
Using the code:
GridData toolBarlayout = new GridData(SWT.LEFT, SWT.TOP, true, true);
toolBarlayout.heightHint = link.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
ToolBar toolBar = new ToolBar(composite, SWT.BORDER | SWT.VERTICAL);
toolBar.setLayoutData(toolBarlayout);
ToolItem item = new ToolItem(toolBar, SWT.PUSH|SWT.TOP);
item.setText("toolbar");
The output is something like:
The GridLayout will allow the link widgets to grow to the height of the row, and will size the height of the row to accommodate the toolbar, and the toolbar wants to be at least as big as it is in your output. For that reason, you might have to explicitly set the height you want for the ToolBar (in pixels) rather than taking it from the Link. But you can put some logging in to see what computeSize() is actually returning.
I would create a GridData for the ToolBar, set the heightHint on the GridData to the height you want, and call toolBar.setLayoutData.
GridData toolBarLayoutData = new GridData();
toolBarLayoutData.heightHint = ...;
toolBar.setLayoutData(toolBaryLayoutData);
Related
I have the following problem:
I'm using SWT to create a GUI for my application. I have a TabFolder in which I add several TabItems and in each of those I create a ScrolledComposite that holds some content.
The TabFolder is displaying fine, however the ScrolledComposite in the TabFolder does only show it's content in the first TabItem. All other ScrolledComposites are visible themselves just fine but their content is invisible.
Here is a little code snippet that demonstrates what I am referring to:
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.H_SCROLL | SWT.V_SCROLL );
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
You can tell that the content is being displayed if the area is painted red. If the content is invisible the area is blue (the background of the ScrolledComposite)
I'm not sure if that matters but this occurs on Linux Mint 18 and it appears to only happen within GTK 3 (in 2 it works just fine)
After quite some time I tracked the issue down to the following:
It turned out that the problem was that the "missing" content had a size of zero, because the layouting doesn't set the size of those.
In my case it could be fixed by removing the SWT.V_SCROLL and the SWT.H_SCROLL style constants from the ScrolledComposite. Therefore the above code written as following works as expected.
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.NONE);
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
Although that causes all content to be properly sized it completely removes the ScrollBars of the ScrolledComposite which somehow is not what you want from a ScrolledComposite.
Does anyone know how to fix that or whether that is a bug (that might have been fixed in newer SWT versions)?
I've fixed bugs related to this a few months ago.
Can you try latest SWT master?
http://download.eclipse.org/eclipse/downloads/
I want to manage the position of component in GUI by easily drag and drop them at design page of Eclipse. This is the GUI I see in design page.(Right click>Test/Preview)
I think after I finish rearrange the component in design page, it will look similar when I RUN the app. But, this GUI appear.
The different appearance make me very hard to adjust the component, for example the width of DAY 2, the height of Medication, DOB : and the green background.
Please let me know if there is any solution to this problem. Thanks.
You can use the GridLayout where each day is a column and the subjects(diagnosis, treatment and so on) are the rows. And you create a composite in each grid cell (like Day1 & diagnosis) which contains your buttons for this day and the subject.
[EDIT]
My suggested implementation is: (SWT.BORDER marks all cells of the toplevel grid)
GridLayout topLevelLayout = new GridLayout();
topLevelLayout.numColumns = 4;
parent.setLayout(topLevelLayout);
// head row
Label label = new Label(parent, SWT.BORDER);
label.setText("Activity");
label = new Label(parent, SWT.BORDER);
label.setText("Day 1");
label = new Label(parent, SWT.BORDER);
label.setText("Day 2");
label = new Label(parent, SWT.BORDER);
label.setText("Day 3");
// new row - first cell
label = new Label(parent, SWT.BORDER);
label.setText("Diagnosis");
// Day1 & Diagnosis
GridLayout cellLayout = new GridLayout();
cellLayout.numColumns = 2;
Composite composite = new Composite(parent, SWT.BORDER);
composite.setLayout(cellLayout);
Button button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Blood Pressure");
button = new Button(composite, SWT.NONE);
button.setText("Vital signs");
// other subjects of diagnosis at day 1...
// Day2 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Blood pressure");
// other subjects of diagnosis at day 2...
// Day3 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("Stress Tests");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Cardiac rhythm");
// other subjects of diagnosis at day 1...
label = new Label(parent, SWT.BORDER);
label.setText("Treatment");
Things like this happen with any designer. Usually they are an indicator of a location change occuring at runtime as the result of a dynamic parameter of some kind. Make sure that you having the correct padding on each of the buttons, and whatever you are using to contain the buttons in should be using a consistent spacing for each one. Also make sure that alignment within the containers is correct, and that both your buttons and the containers they are in are properly anchored.
at the moment I'm playing a little bit around with SWT and try to implement an expandbar:
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
ExpandBar bar = new ExpandBar(container, SWT.V_SCROLL);
Composite newConstraint = new Composite(bar, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
newConstraint.setLayout(gridLayout);
Label sourcelbl = new Label(newConstraint, SWT.NONE);
sourcelbl.setImage(getMyImage("source.png"));
sourcelbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
true, false));
Label spacelbl = new Label(newConstraint, SWT.NONE);
Label targetlbl = new Label(newConstraint, SWT.NONE);
targetlbl.setImage(getMyImage("target.png"));
targetlbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
true, false));
ListViewer lvSource = new ListViewer(newConstraint);
lvSource.setContentProvider(new ConstraintDialogContentProvider());
lvSource.setLabelProvider(new ConstraintDialogLabelProvider());
lvSource.setInput(fm);
Combo constrainType = new Combo(newConstraint, SWT.NONE);
constrainType.setItems(new String[] { "require", "exclude" });
constrainType.setLayoutData(new GridData(GridData.CENTER,
GridData.CENTER, true, false));
ListViewer lvTarget = new ListViewer(newConstraint);
lvTarget.setContentProvider(new ConstraintDialogContentProvider());
lvTarget.setLabelProvider(new ConstraintDialogLabelProvider());
lvTarget.setInput(fm);
ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
item0.setText("New Constraint");
item0.setHeight(newConstraint.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item0.setControl(newConstraint);
omposite existingConstraints = new Composite(bar, SWT.NONE);
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
existingConstraints.setLayout(gridLayout);
........
ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1);
item1.setText("Existing Constraints");
item1.setHeight(existingConstraints.computeSize(SWT.DEFAULT,
SWT.DEFAULT).y);
item1.setControl(existingConstraints);
item0.setExpanded(true);
bar.setSpacing(8);
return container;
}
The problen then is the width, which doesn't seems to be computed:
Without the extendbar, everything works fine and the width is automatically computed:
How can I let the extendbar compute the width???
Cheers,
Phil
The ExpandBar size computation only appears to take the size of the ExpandItem text in to account and ignores the size of the control in the item.
You would have to override the ExpandBar.computeSize method to change this (and also ExpandBar.checkSubclass to stop that rejecting your subclass).
Or you could just include spaces at the end of the text for the ExpandItems to fudge the calculated size.
Based on greg-449's answer, I found another workaround, which is to set a widthHint for the ExpandBar:
ExpandBar expandBar = new ExpandBar(this, SWT.BORDER);
GridData gridDataExpandBar = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
gridDataExpandBar.widthHint = 150;
I created a toolbar with text field and two toolItem
The problem that the Text is exist in the right scrren and not in the left screen.
How I can put the text in the right screen ?
It should look :
Text item1 item2
ToolBar treeToolBar = new ToolBar(treeComposite, SWT.RIGHT_TO_LEFT);
Text text = new Text(treeToolBar, SWT.NONE);
text.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, false, false));
text.setText("Text");
text.pack();
item1 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
item2 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
treeToolBar.pack();
After you create the text, you also have to create a tab item for it. Remove the text.pack() call and insert this:
ToolItem textItem = new ToolItem(treeToolBar, SWT.SEPARATOR);
textItem.setControl(text);
textItem.setWidth(text.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
This is how ControlContribution does in method fill(ToolBar parent, int index).
I have two labels placed in the gridLayout. label 1 is just one word and the label 2 is 4 lines.
Since the label 2 is 4 lines, label 1 is verticaly centered, but I want that to be vertically in the top.
Below is the label settings I have used.
Label label = new Label(parent, SWT.WRAP);
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false);
gd.widthHint = 200;
label.setLayoutData(gd);
Kindly help me in placing the label alignment on top not in the center for label 1
If you look at the Javadoc of GridData.HORIZONTAL_ALIGN_BEGINNING and GridData.VERTICAL_ALIGN_BEGINNING, you can see that it says:
Not recommended. Use new GridData(SWT.BEGINNING, int, boolean, boolean) instead.
and
Not recommended. Use new GridData(int, SWT.BEGINNING, boolean, boolean) instead.
Always use the SWT alignment constants:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(2, false));
Label left = new Label(shell, SWT.BORDER);
left.setText("LEFT");
Label right = new Label(shell, SWT.BORDER);
right.setText("RIGHT\nRIGHT\nRIGHT\nRIGHT");
GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
left.setLayoutData(data);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
right.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Looks like this: