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));
Related
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 want to span right 2 column of 3 column in grid layout, but I do not know how to do, someone can give me some guidance on how to do ?
Example code and image is below:
#PostConstruct
public void PostConstruct(Composite parent) {
toolkit = new FormToolkit(Display.getDefault());
form = new ScrolledForm(parent, SWT.V_SCROLL);
form.setBounds(0, 0, 650, 478);
form.setExpandHorizontal(true);
form.setExpandVertical(true);
form.setBackground(toolkit.getColors().getBackground());
form.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
form.setFont(JFaceResources.getHeaderFont());
form.setText("DOCUMENTATIONS");
toolkit.decorateFormHeading(form.getForm());
GridLayout cl = new GridLayout(3, true);
form.getBody().setLayout(cl);
sectionSelection = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
sectionSelection.setText("");
sectionClientSelection = toolkit.createComposite(sectionSelection);
sectionClientSelection.setLayout(new GridLayout());
sectionClientSelection.setLayoutData(new GridData(100, 100));
sectionSelection.setClient(sectionClientSelection);
createSelectionSection();
sectionDefinition = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
sectionDefinition.setText("");
sectionClientDefinition = toolkit.createComposite(sectionDefinition);
sectionClientDefinition.setLayout(new GridLayout());
GridData gridData = new GridData(SWT.FILL,GridData.VERTICAL_ALIGN_END);
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
sectionClientDefinition.setLayoutData(gridData);
sectionDefinition.setClient(sectionClientDefinition);
createDefinitionSection();
}
You need to set the layout data for the Section rather than the Composite in the section.
sectionDefinition.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
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.
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: