Why SWT List fills all composite? - java

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.

Related

SWT Issue in Aligning + Filling a Label with GridData

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));

SWT- setSize and setBounds doesn't work on label

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;
}

How to reduce table size in Table viewer in RCP?

i have GridTableViewer, in that table showing rows are much.so i planned to reduce table height. But i am unable reduce the tableviewer size of the height.Can u anyone help me in this?
///pasting sample code
#Override
protected void addComponents(FormToolkit toolkit) {
myTableViewr=
new ExtendedGridTableViewer(client, getPage(), SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
Grid grid = myTableViewr.getGrid();
toolkit.adapt(grid, false, false);
grid.setHeaderVisible(true);
grid.setCellSelectionEnabled(false);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
grid.setLayoutData(gridData);
List<ColumnType<?>> columns = new ArrayList<ColumnType<?>>();
columns.add(new nameColumnType());
columns.add(new valueColumnType());
myTableViewr.createColumnGrid(toolkit, columns);
//myModel - my model code
LogoInsertionContentProvider contentProvide = new LogoInsertionContentProvider(this, myTableViewr, myModel);
myTableViewr.setContentProvider(contentProvide);
myTableViewr.setInput(myModel);
}
here, two columns and 3 rows only. but i am getting more than 10 rows
Your GridData for the table is:
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
which is asking to grab all available space. If you want to use a fixed height for the table use something like:
GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
gridData.heightHint = 500; // The height you want

SWT: Width of expandbar

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;

SWT.Table behaves differently with same code (Eclipse RCP)

Every second line is coloured on the table in the view.
I have the same table twice on the editor, but it doesn't show this separation:
My code of the lower table in the editor:
table1 = new Table(c, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION
| SWT.V_SCROLL);
TableLayout layout1 = new TableLayout();
table1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
table1.setLayout(layout1);
table1.setLinesVisible(true);
table1.setHeaderVisible(true);
TableColumn colReihe = new TableColumn(table1, SWT.LEFT);
colReihe.setText("Reihe");
TableColumn colPlatz = new TableColumn(table1, SWT.LEFT);
colPlatz.setText("Platz");
TableColumn colPreis = new TableColumn(table1, SWT.LEFT);
colPreis.setText("Preis");
TableColumn colStatus = new TableColumn(table1, SWT.LEFT);
colStatus.setText("verkauft");
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
....some more code here....
this.toolkit.adapt(table1, true, true);
I don't really understand your question. Do you expect the table on the lower right of your screenshot to look like the table on the left? If that's the question, check, whether you call toolkit.adapt(table, true, true); with the left table as well, or try to remove that call with the right table. The FormToolkit sets a lot of colors and styles on widgets.

Categories