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:
Related
So, I tried to make a little game where you have to do actions that are displayed by a label.
My problem is that I can't align the label in the center of a column AND fill the whole space of the column with the label so I can use the background cColor.
For example:
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
Font font = new Font(display, "Arial", 30, SWT.NONE);
layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
shell.setText("ReactionGame 2.0");
shell.setMaximized(true);
shell.setLayout(layout);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
GridData data1 = new GridData(SWT.CENTER, SWT.CENTER, true, true);
data1.verticalAlignment = GridData.FILL;
data1.horizontalAlignment = GridData.FILL;
Label lifelabel = new Label(shell, SWT.NONE);
lifes(lifelabel);
lifelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
lifelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
lifelabel.setFont(font);
lifelabel.setLayoutData(data1);
Label scorelabel = new Label(shell, SWT.NONE);
scorecount(scorelabel);
scorelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
scorelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
scorelabel.setFont(font);
scorelabel.setLayoutData(data1);
Label gamelabel = new Label(shell, SWT.NONE);
gamelabel.setText("");
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gamelabel.setFont(font);
gamelabel.setLayoutData(data1);
This code gives me a design of this:
My question now is how can I align the text of the label in the center?
You are reusing the same GridData (data1) for multiple controls - this is not allowed. The GridData instance is used to save layout information about individual controls, you must use a new instance for each control.
So use:
lifelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
scorelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
To make the third area you will need to use an extra Composite with the label centred in that and a background colour set:
Composite gameComp = new Composite(shell, SWT.NONE);
gameComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
gameComp.setBackground(display.getSystemColor(SWT.COLOR_RED));
GridLayout gameLayout = new GridLayout();
gameLayout.marginHeight = 0;
gameLayout.marginWidth = 0;
gameComp.setLayout(gameLayout);
Label gamelabel = new Label(gameComp, SWT.NONE);
gamelabel.setText("game");
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_RED));
gamelabel.setFont(font);
gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
I have a label. Sometimes, the text in it has overflow and doesn't show in the label. So, I would like to change the height of the label and also would like to when it has overflow it shows in its below line. How can I do that?
composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout());
GridData data = new GridData(GridData.FILL_HORIZONTAL);
Group grpModelProperties = new Group(composite, SWT.SHADOW_IN);
grpModelProperties.setLayout(new GridLayout(2, false));
grpModelProperties.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
Label l2 = new Label(grpModelProperties, SWT.NULL);
new Label(grpModelProperties, SWT.NONE);
l2.setLayoutData(data);
l2.setText("Text: " + Text);
//l2.setBounds(0, 0, 470, 200);
//l2.setSize( 470, 400 );
You can pass the style SWT.WRAP to the label constructor to specify that the text should automatically wrap in a new line if there is not enough horizontal space:
Label l2 = new Label(grpModelProperties, SWT.WRAP);
There are a couple of changes to make for this to work. First, you'll need to use the SWT.WRAP style bit in the Label constructor. In your case:
Label l2 = new Label(grpModelProperties, SWT.WRAP);
(As a side note, if you don't want a style set, you should use SWT.NONE, not SWT.NULL).
Also, you'll need to set a widthHint on the layout data for the top level element, otherwise your dialog will expand to accommodate the full width of the text. In your code:
composite.setLayout(new GridLayout());
GridData compositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
compositeGridData.widthHint = 200;
composite.setLayoutData(compositeGridData);
Slightly cleaned up, but your code should ultimately look something like this:
#Override
protected Control createDialogArea(Composite parent) {
composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout());
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.widthHint = 200;
composite.setLayoutData(data);
Group groupModelProperties = new Group(composite, SWT.SHADOW_IN);
groupModelProperties.setLayout(new GridLayout(2, false));
groupModelProperties.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
Label label = new Label(groupModelProperties, SWT.WRAP);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
label.setText("Text: The quick brown fox jumps over the lazy dog. This is a really long line of text that should wrap into a new line.");
return composite;
}
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);
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;
Hi I have a problem with the layouts in SWT, I want to create an application with 4 equal sized lists. When the lists are empty, each looks good, but when I will fulfill one of the lists with data that covers the entire composite.
and lists with data:
I know that it by wrong layout, so I'll show you some source:
shlPirotechnikafcwnxrap.setLayout(new FillLayout(SWT.HORIZONTAL));
......
Composite mainComposite = new Composite(shlPirotechnikafcwnxrap,
SWT.NONE);
mainComposite.setLayout(new FormLayout());
......
Composite rightCenterComposite = new Composite(mainComposite, SWT.NONE);
rightCenterComposite.setLayoutData(new FormData());
rightCenterComposite.setLayout(new GridLayout());
rightCenterComposite.setLayoutData(GUIHelper.getFormData(5, 100, 30, 100));
TabFolder tabFolder = new TabFolder(rightCenterComposite, SWT.NONE);
tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
/**
* TAB ITEM
*/
TabItem tbtmSql = new TabItem(tabFolder, SWT.NONE);
tbtmSql.setText("SQL");
/**
* Composite inside TabItem
*/
Composite sqlTabComposite = new Composite(tabFolder, SWT.NONE);
GridLayout gl_sqlTabComposite = new GridLayout();
gl_sqlTabComposite.makeColumnsEqualWidth = true;
gl_sqlTabComposite.numColumns = 2;
sqlTabComposite.setLayout(gl_sqlTabComposite);
sqlTabComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
tbtmSql.setControl(sqlTabComposite);
Group grpSowaKluczoweSql = new Group(sqlTabComposite, SWT.NONE);
grpSowaKluczoweSql.setLayout(new FormLayout());
grpSowaKluczoweSql.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
grpSowaKluczoweSql.setText("S\u0142owa kluczowe i operatory SQL:");
List listSlowaKluczoweSql = new List(grpSowaKluczoweSql, SWT.BORDER | SWT.V_SCROLL);
listSlowaKluczoweSql.setLayoutData(new FormData());
listSlowaKluczoweSql.setLayoutData(GUIHelper.getFormData(0, 100, 0, 100));
Group grpTabele = new Group(sqlTabComposite, SWT.NONE);
grpTabele.setLayout(new FormLayout());
grpTabele.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
grpTabele.setText("Nazwy tabel:");
List listTabele = new List(grpTabele, SWT.BORDER | SWT.V_SCROLL);
listTabele.setLayoutData(new FormData());
listTabele.setLayoutData(GUIHelper.getFormData(0, 100, 0, 100));
It is because of this: sqlTabComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); You are using FILL + grab excess space. Try using appropriate layout constants, like BEGINNING for horizontal alignment and TOP for vertical. Also set grabExcessVerticalSpace to false. Check all related places.