Why composite is expanded by text field? - eclipse

I created a text field which used GridLayout and I set the 3rd parameter of the GridData (grabExcessHorizontalSpace) to true. The Text field is not getting wrap into multiple line when I set a very long sequence of non-whitespace characters won't be split into lines. The Composite is expanded by text filed.
Below is my code:
Composite composite = new Composite(m_shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.LEAD);
label.setText("Label");
label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
Text text = new Text(composite,SWT.MULTI|SWT.BORDER|SWT.WRAP|SWT.V_SCROLL);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd.heightHint = 2 * text.getLineHeight();
text.setLayoutData(gd);
text.setText("trweghjfbfdshjfghjreuyhjfghkjhjhjsdghjreewhjdfghjhjgfdhjsdghjtreuytrehjdfghjhjdfgh8irtrhjghjhjdfghjdfghjfghjdfg");

I assume you are calling pack on the Shell to complete the layout. This causes controls to be sized at their preferred size - the preferred size for a Text control is the size with no wrapping.
If you want the shell to be a specific size you can layout on the shell without calling pack. The text should then wrap.
Alternatively specify a widthHint for the Text control.

Setting widthHint to 0 together with the horizontalAlignment to SWT.FILL makes the text fill all availible space in its column, but not grow more that that. Instead it wraps as desired:
I tried this on Windows 10 with SWT 3.103.2. Greg seems to have got another result on Mac.
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.LEAD);
label.setText("Label");
label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd.heightHint = 2 * text.getLineHeight();
// Added widthHint
gd.widthHint = 0;
text.setLayoutData(gd);
text.setText("trweghjfbfdshjfghjreuyhjfghkjhjhjsdghjreewhjdfghjhjgfdhjsdghjtreuytrehjdfghjhjdfgh8irtrhjghjhjdfghjdfghjfghjdfg");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

Related

Hiding SWT Controls

How we can hide the SWT controls?
I know setVisible() method of control class can be used. But the disadvantage of it is, hidden widget will not released and can't be used by other widgets.
Is there any other approach which can be adopted?
You can use layout data. In case of GridLayout, you can use exclude specific widget from being drown on canvas.
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(4, false));
Label hidenLabel = new Label (comp, SWT.NONE);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
hidenLabel.setGridData(gridData );
//hide the button
gridData .exclude = true;
comp.pack();

How to set image to SWT. Group

I have a group in SWT composite.I want to set an image to it,instead of a text.How can I achieve this?I am not finding any direct way of setting an image to that .But is there any workaround for this?
Below is the snippet
final Group defineInteractionGroup = new Group(sashForm, SWT.NONE);
Image image = new Image(Display.getCurrent(), "C:/myIcon.png");
GridData interactionsGroupGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
interactionsGroupGridData.heightHint = 0;
defineInteractionGroup.setLayoutData(interactionsGroupGridData);
defineInteractionGroup.setLayout(new GridLayout(1, false));
defineInteractionGroup
.setText(ResourceAssistant.getString(InteractionExpandedComposite.class, "defineInteraction"));

Use FillLayout with Browser in SWT

I have a view that consists of a heading and a browser. I want the browser to fill my view. This is how it should look:
I have tried out different combinations.
c = new Composite(parent, SWT.NONE);
c.setLayout(new RowLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
Composite wrapper = new Composite(c, SWT.NONE);
wrapper.setBackground(new Color(null, 0, 0, 0));
wrapper.setLayout(new FillLayout(SWT.VERTICAL));
browser = new Browser(wrapper, SWT.WRAP);
browser.setBackground(color);
produces:
Setting a FillLayout to c does not work either. By doing so both, heading and browser, share the view's size and have an equal size.
c = new Composite(parent, SWT.NONE);
c.setLayout(new FillLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
browser = new Browser(c, SWT.WRAP);
browser.setBackground(color);
produces:
I have also tried the following:
composite with filllayout
-> composite with gridlayout
->composite with rowLayout (fixed height, grab right)
->composite with rowLayout
-> composite with fillLayout
-> composite with browser
Assuming that the header and browser have the same parent, you could use a GridLayout like this:
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.FILL, SWT.TOP, true, false ) );
browser.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
Even though a one-cell-grid isn't really what a GridLayout is meant for, it should do the trick. The layout data advise the label to align at the top and use all horizontal space and the browser to use the horizontal and vertical space

Jface tableviewer - Scroll bars are not appearing when the window is being resized

I am using a jface tableviewer with a tablecolumnlayout (for it's parent composite) in my eclipse RCP application as follows -
Composite composite = new Composite(parent, SWT.NONE);
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
GridDataFactory.swtDefaults().grab(true, true).applyTo(composite);
// Table Composite
Composite tblComposite = new Composite(composite, SWT.NONE);
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(tblComposite);
GridDataFactory.swtDefaults().grab(true, true).hint(500, 180).applyTo(tblComposite);
// Table Column Layout
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tblComposite.setLayout(tableColumnLayout);
tableViewer =
new TableViewer(tblComposite, SWT.BORDER | SWT.FULL_SELECTION |
SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);
Table table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
// Column 1
TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);
column.getColumn().setText("Source");
tableColumnLayout.setColumnData(column.getColumn(), new ColumnWeightData(50, true));
// Column 2
TableViewerColumn column2 = new TableViewerColumn(tableViewer, SWT.NONE);
column2.getColumn().setText("Target");
tableColumnLayout.setColumnData(column2.getColumn(), new ColumnWeightData(50, true));
tableViewer.setUseHashlookup(true);
tableViewer.setContentProvider(new ArrayContentProvider());
tableViewer.setLabelProvider(new CustomLabelProvider());
tableViewer.setSorter(new CustomViewSorter());
The problem I'm facing is that when I resize the window (reduce the size of the window) I'm not seeing Vertical and Horizontal scrollbars for the table viewer, that is even when the rows of the table are not seen when resizing (shrinking the size until some rows from table are hidden), the scrollbars don't appear at all but we generally expect them to appear right?
Is there anything wrong in GridData logic I have used? Please help me get unstuck here.
Remove SWT.FULL_SELECTION. It does not work with SWT.V_SCROLL | SWT.H_SCROLL.

How to put label item on rowLayout in SWT?

I want to put some items on row layout. But I take an error. My error image is here:
my example code is below:
GridLayout parentLayout = new GridLayout(1, true);
parent.setLayout(parentLayout);
// design filter composite layout
Composite filterComposite = new Composite(parent, SWT.NONE);
RowLayout filterCompositeLayout = new RowLayout();
filterCompositeLayout.wrap = true;
filterCompositeLayout.pack = false;
filterCompositeLayout.justify = true;
filterCompositeLayout.type = SWT.HORIZONTAL;
filterComposite.setLayout(filterCompositeLayout);
// design filter composite
Label lbl_type = new Label(filterComposite, SWT.BORDER);
lbl_type.setText("Type :");
Combo cmb_type = new Combo(filterComposite, SWT.BORDER);
cmb_type.setText("-- choose --");
Label lbl_severity = new Label(filterComposite, SWT.BORDER);
lbl_severity.setText("Severity :");
Combo cmb_severity = new Combo(filterComposite, SWT.BORDER);
cmb_severity.setText("-- choose --");
Label lbl_startDate = new Label(filterComposite, SWT.BORDER);
lbl_startDate.setText("Start Date : ");
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
dateTimeStart.setLayoutData(new GridData(SWT.NONE, SWT.NONE, true, true));
Error is on DataTime row. Can anybody give some advice? Thank you.
You cant set GridData to component placed in RowLayout. You should use RowData instead:
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
RowData rd = new RowData();
rd.width = 123;
rd.height = 23;
dateTimeStart.setLayoutData(rd);
You are attempting to set GridData layout data for dateTimeStart. However Composite filterComposite has RowLayout and this Layout expects RowData layout data.
example:
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
dateTimeStart.setLayoutData(new RowData (width, height)); //set proper height and width according to your UI expectations