I am having a problem getting SWT Text controls to lay out the way I want them to within a group. Specifically, I'm having a difficult time getting the verticalSpan passed to the GridData to be reflected in the GUI asset. In the example below, the 3 controls that I am unable to get to display correctly are descriptionText, defaultActionText, and defaultReportActionText, but translationText display displays correctly. I'm not sure what I'm doing incorrectly here so I appreciate any feedback!
Group:
paramsFieldComposite = new Group( upperRightComposite, SWT.BORDER );
// TODO: Change group to composite and remove .setText()
paramsFieldComposite.setText( "paramsFieldComposite" );
paramsFieldComposite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true, 1, 1 ) );
paramsFieldComposite.setLayout( new GridLayout( 2, true ) );
Translation Controls (working as I would expect with a width of 1 and height of 3):
Label hostnameLabel = new Label( paramsFieldComposite, SWT.NONE );
hostnameLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, false, false, 1, 1 ) );
hostnameLabel.setText( configResourceBundle.geti18nDisplay( "HostnameLabel" ) );
Label translationLabel = new Label( paramsFieldComposite, SWT.NONE );
translationLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 1 ) );
translationLabel.setText( configResourceBundle.geti18nDisplay( "TranslationLabel" ) );
hostnameText = new TextControl( paramsFieldComposite, SWT.BORDER );
hostnameText.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 1 ) );
hostnameText.setEditable( true );
translationText = new TextControl( paramsFieldComposite, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP );
translationText.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 3 ) );
translationText.setEditable( true );
Description/Action Controls (not working as I'm expecting, I want them to all have a height of 3 instead of 1):
Label descriptionLabel = new Label( paramsFieldComposite, SWT.NONE );
descriptionLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 2, 1 ) );
descriptionLabel.setText( configResourceBundle.geti18nDisplay( "DescriptionLabel" ) );
descriptionText = new TextControl( paramsFieldComposite, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI );
descriptionText.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true, 2, 3 ) );
descriptionText.setEditable( true );
Label defaultActionLabel = new Label( paramsFieldComposite, SWT.NONE );
defaultActionLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 1 ) );
defaultActionLabel.setText( configResourceBundle.geti18nDisplay( "DefaultActionLabel" ) );
Label defaultReportActionLabel = new Label( paramsFieldComposite, SWT.NONE );
defaultReportActionLabel.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 1 ) );
defaultReportActionLabel.setText( configResourceBundle.geti18nDisplay( "DefaultReportActionLabel" ) );
defaultActionText = new TextControl( paramsFieldComposite, SWT.BORDER | SWT.V_SCROLL );
defaultActionText.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 3 ) );
defaultActionText.setEditable( true );
defaultReportActionText = new TextControl( paramsFieldComposite, SWT.BORDER | SWT.V_SCROLL );
defaultReportActionText.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, false, 1, 3 ) );
defaultReportActionText.setEditable( true );
Here is what the GUI looks like.
The difference between the behaviour of translationText and defaultActionText is quite easy to explain.
You told both to span 3 rows. However, while the height of these three rows of translationText is defined by other Widgets (hostnameText, instanceLabel, instanceText), this is not the case for defaultActionText. There are no other widgets in these three rows (except for defaultReportActionText). So the height will be set to the necessary height.
You can manually increase this height by setting the heightHint of the GridData:
GridData data = new GridData( SWT.FILL, SWT.FILL, true, false, 1, 3 );
data.heightHint = 80;
defaultActionText.setLayoutData(data);
defaultReportActionText.setLayoutData(data);
The result can be seen here:
To get a more exact result for the height, you could use:
data.heightHint = hostnameText.computeSize(SWT.DEFAULT, SWT.DEFAULT).y
+ instanceLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y
+ instanceText.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
Related
I have written this stuff but it won't provide me the resizable feature of table.
Composite resultTableComposite = getToolkit().createComposite( parent );
GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true, 1, 1 );
gridData.heightHint = 300;
gridData.widthHint = 250;
resultTableComposite.setLayoutData( gridData );
TableColumnLayout tableLayout = new TableColumnLayout();
resultTableComposite.setLayout( tableLayout );
resultTable = new TableViewer( resultTableComposite,
SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION );
getToolkit().adapt( resultTable.getTable() );
resultTable.getTable().setLinesVisible( true );
resultTable.getTable().setHeaderVisible( true );
The following image shows only 3 rows after filling 3 rows scroll added, but if we want to see 4 elements without scrolling we cant see. we want we can able to expand on resize the table:
these are just sample codes. I created a GridLayout composite and removed the margins. But there are still spaces in between the rows.
Composite composite = new Composite(shell, SWT.NONE);
GridLayout gl_composite = new GridLayout(1, false);
gl_composite.marginWidth = 0;
gl_composite.marginHeight = 0;
composite.setLayout(gl_composite);
Composite composite_1 = new Composite(composite, SWT.BORDER);
GridData gd_composite_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_composite_1.heightHint = 52;
gd_composite_1.widthHint = 802;
composite_1.setLayoutData(gd_composite_1);
composite_1.setLayout(new GridLayout(1, false));
Composite composite_2 = new Composite(composite, SWT.BORDER);
GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_composite_2.widthHint = 803;
composite_2.setLayoutData(gd_composite_2);
I need the rows to not have any spaces in between. The composites are bordered since I need to have a line below . I'm new to SWT so..
Thanks!!
The verticalSpacing field of GridLayout sets the spacing between rows, so:
gl_composite.verticalSpacing = 0;
Use horizontalSpacing for spacing between columns.
The default for both values is 5
I have some trouble with Composite. I have written the following code:
final Composite mavniOptionsComposite = new Composite(parent, SWT.NONE);
mavniOptionsComposite.setLayout(new GridLayout(3, false));
final GridData textGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
final Label mavniFormatingLabel = new Label(mavniOptionsComposite, SWT.None);
mavniFormatingLabel.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniFormatingLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
final GridData mavniFormatingGridData = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
final Button mavniHistoryRunCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniHistoryRunCheckbox.setLayoutData(mavniFormatingGridData);
mavniHistoryRunCheckbox.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniHistoryRunCheckbox.addSelectionListener(new mavniHistoryCheckBoxSelectionAdapter());
widgetsMap.put(PFT.MAVNI_HISTORY, mavniHistoryRunCheckbox);
final Button mavniRealtimeCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniRealtimeCheckbox.setLayoutData(mavniFormatingGridData);
mavniRealtimeCheckbox.setText(PFT.MAVNI_REALTIME.getExplanationLabelText());
mavniRealtimeCheckbox.setEnabled(MyPreferences.isWmcDefined());
widgetsMap.put(PFT.MAVNI_REALTIME, mavniRealtimeCheckbox);
final Label toolNameLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolNameLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolNameLabel.setText(PFT.TOOL_NAME.getExplanationLabelText());
final Text toolNameText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolNameText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_NAME, toolNameText);
final Label toolVersionLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolVersionLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolVersionLabel.setText(PFT.TOOL_VERSION.getExplanationLabelText());
final Text toolVersionText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolVersionText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_VERSION, toolVersionText);
mavniOptionsComposite.setLayout(new GridLayout(2, false));
new MavniOptionViewer(MyPreferences.getMavniOptionsList()).createViewer(mavniOptionsComposite);
createExpandItem(parent, mavniOptionsComposite, "Mavni Options", com.mavni.ui.Activator
.getDefault().getImageRegistry().get(IImageKeys.EXECUTE.getPath()), false);
But I get the following behavior:
I would like the table to expand to on the full layout. As I understand that happens because I set setLayout(new GridLayout(3, false)); and the table contains only two columns. If I set mavniOptionsComposite.setLayout(new GridLayout(2, false)); then the table works as expected but the other elements are showing no in there place (like it should be now).
The wanted behviour:
At first I though to create two GridLayouts and insert them into one composite. I tried to search for a way to combine two layouts in one composite but could not find a way. I need one composite because createExpandItem works with only one composite. How to solve it?
EDIT: In the MavniOptionViewer I have:
public void createViewer(final Composite parent) {
commandsTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
commandsTable.setHeaderVisible(true);
commandsTable.setLinesVisible(true);
final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 5);
data.heightHint = 120;
commandsTable.setLayoutData(data);
final Map<ButtonTypeEnum, Button> buttons = ButtonComboFactory.createAddRemoveCombo(parent);
final Button add = buttons.get(ButtonTypeEnum.ADD);
final Button remove = buttons.get(ButtonTypeEnum.REMOVE);
...
I though of another possible solution. Is it possbile to change createViewer so the table will have 3 columns and the add remove buttons will be on seprated line. How to do it?
A Composite can only have one layout, you can't change it halfway. It will just end up using the last layout you set for everything.
You could use multiple Composites in a containing Composite.
You could keep your main Composite with a GridLayout of 3 columns and tell the table to occupy all 3 of these columns instead of 2 like you are doing now.
You can do that by changing the horizontal span of the table GridData from 2 to 3:
final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5);
Also, you should not reuse GridData objects; from the documentation:
Every control in a Composite that is managed by a GridLayout must have
a unique GridData object.
Is it possible to center the text inside a label vertically? I'm using:
message1 = new Label(shell, SWT.VERTICAL | SWT.CENTER);
This centers the text horizontally, but not vertically.
CLabel label = new CLabel(shell, SWT.CENTER);
Maybe you should try using org.eclipse.swt.custom.CLabel (JavaDoc).
The code above exactly centers the text in the label vertically.
The text within a label is always aligned at the top. The VERTICAL style only applies when SEPARATOR is set. In this case it displays a single vertical or horizontal line and the text is ignored.
But you can center the label itself within the parent. For example, this snippet centers a label within the containing shell by using a GridLayout:
public static void main( String[] args ) {
Display display = new Display();
Shell parent = new Shell( display );
Label label = new Label( parent, SWT.NONE );
label.setText( "some text" );
label.setBackground( display.getSystemColor( SWT.COLOR_GREEN ) );
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true ) );
parent.open();
while( !parent.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
I am wanting to make each cell in a row a different length..
Here is a picture to help.
So the Policy cell and the text to the right is fine. However, Section 1 and Section 2 I want to be 50/50.. Not 20/80 or whatever it is now. I have started using the WindowsBuilder tool instead of doing this by hand. Is this possible to do?
To lay out controls in the requested manner with a GridLayout in SWT you will have to group the controls of each row into a composite of their own like so:
shell.setLayout( new RowLayout( SWT.VERTICAL ) );
Composite composite1 = new Composite( shell, SWT.NONE );
composite1.setLayout( new GridLayout( 2, false ) );
createLabel( composite1, "2020" );
createLabel( composite1, "808080808080" );
Composite composite2 = new Composite( shell, SWT.NONE );
composite2.setLayout( new GridLayout( 2, false ) );
createLabel( composite2, "50505050" );
createLabel( composite2, "50505050" );
private static Label createLabel( Composite parent, String text ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
return label;
}
However, to me a FormLayout seems more suitable to solve the given problem:
shell.setLayout( new FormLayout() );
FormData leftFormData = new FormData();
leftFormData.top = new FormAttachment( 0 );
leftFormData.left = new FormAttachment( 0 );
leftFormData.right = new FormAttachment( 20 );
Label leftLabel = createLabel( shell, "2020", leftFormData );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( 0 );
rightFormData.left = new FormAttachment( leftLabel );
rightFormData.right = new FormAttachment( 100 );
createLabel( shell, "808080808080", rightFormData );
private static Label createLabel( Composite parent, String text, Object layoutData ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
label.setLayoutData( layoutData );
return label;
}
If you find the formData and formAttachment code too verbose, you may have a look at Slim Down SWT FormLayout Usage
And for an in-depth discussion of SWT layouts I recommend the Understanding Layouts in SWT article.
EDIT: Missed that you are using SWT GridLayout instead of the Swing one. My solution works with Swing so keep that in mind.
You may want to use the GridBagLayout. It's more complicated than the GridLayout but offers greater flexibility
See some documentation on how to use it here