I'm trying to wrap this text automatically, but it doesn't wrap. I'm trying with the code below, not sure what's wrong with the code. This is
protected Control createContents(Composite parent){
Composite composite = new Composite(parent, SWT.WRAP);
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData(SWT.HORIZONTAL, SWT.TOP, false, false));
Group group = new Group(composite, SWT.NONE);
group.setText("my group");
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
Button button = new Button(group2,SWT.CHECK);
GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, false, true, 1, 1);
button.setLayoutData(data);
Label label= new Label(group, SWT.WRAP);
data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("my long text is very long, I need to wrap this very long text lalalalalalalalalalalalalala");
}
The composite's layoutdata also needs to have grabExcessHorizontal space set to true, i.e.:
composite.setLayoutData(new GridData(SWT.NONE, SWT.TOP, true, false));
I think the wrapping label and all of its parent composites need to have that.
By the way, the style-bit SWT.WRAP doesn't make sense for class Composite.
Related
I can't set the size for the button in the below code. If I set b.setSize to (2000,2000) nothing will change. Do you have any suggestions what I'm doing wrong or overlook.
Best regards
Chris
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
GridLayout containerLayout = new GridLayout();
GridData containerLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
container.setLayout(containerLayout);
container.setLayoutData(containerLayoutData);
Tag t = new Tag("Action", 'm');
Button b = new Button(container, SWT.PUSH);
GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
b.setLayoutData(new GridData(SWT.BEGINNING ,SWT.CENTER, false,false));
b.setText("Movie");
Button button2 = new Button(container, SWT.PUSH); button2.setLayoutData(newGridData(SWT.BEGINNING,SWT.CENTER,false,false));
button2.setText("Performer");
return container;
}
You can't mix layouts with setSize - the layout will override the size with the size that it calculates.
Instead specify a width hint for the button layout:
Button b = new Button(container, SWT.PUSH);
GridData data = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
data.widthHint = 100; // Width in pixels
b.setLayoutData(data);
b.setText("Movie");
Since your code is in a JFace Dialog you can also use the convertHorizontalDLUsToPixels or convertWidthInCharsToPixels methods of Dialog to calculate the width. For example:
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
You can set the height using the heightHint in a similar way.
I have a wizard with table and button components. When I click to the add button I choose from dialog window what items should be added to the table. I confirm items, and then this ones appears in the table with scrollbar. But if I resize the wizard, the table size is changed. How to fix it?
Table before resize:
Table after wizard resize
Composite compositeArea = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
compositeArea.setLayout(layout);
Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
new TableColumn(someList, SWT.NULL);
table.setLayoutData(new GridData(HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL));
** Please note that the use of HORIZONTAL_ALIGN_FILL and GRAB_HORIZONTAL is discouraged. Instead, you should be using the public GridData(int, int, boolean, boolean) constructor. **
To slighly simplify your code snippet (only one column on the composite, and only one default table column - see full code below):
// ...
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
// ...
...and we see that the Table isn't fitting in the available space or showing the scrollbar as expected, and the same occurs when the Shell is resized.
In answer to your question, this is because the layout data of the Table doesn't know how to layout vertically - you've only specified two horizontal style attributes.
If we instead use the suggested constructor:
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
...the Table takes the available space correct, shows the scrollbar, and updates correctly when the Shell is resized. Using the layout data, we've told the Table to fill the available horizontal space, and the Table will display the scrollbar if necessary.
Full example:
public class TableResizeTest {
private final Display display;
private final Shell shell;
public TableResizeTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setMaximized(true);
final Composite parent = new Composite(shell, SWT.NONE);
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
parent.setLayout(new GridLayout());
// -- snippet --
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
// table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// -------------
for (int i = 0; i < 20; ++i) {
new TableItem(table, SWT.NONE).setText(String.valueOf(i));
}
}
public void run() {
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TableResizeTest().run();
}
}
Here is my problem : I have been using SWT to build a little GUI for an Eclise Plugin. On the picture below, the parts 1,2,2',3,3' behave exactly as I would like, and fit correctly in the window.
However, the 4th part (which is a TextMergeViewer) keeps it little size in the corner of its container.
The following code shows how, for instance, I am defining the parts 2 and 2' :
Composite viewersContainer;
viewersContainer = new Composite(shell, SWT.BORDER);
viewersContainer.setLayout(new GridLayout(2, false));
viewersContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 400;
data.widthHint = 400;
oldFileViewer = new Browser(viewersContainer, SWT.BORDER);
oldFileViewer.setText("here is the old file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
oldFileViewer.setLayoutData(data);
newFileViewer = new Browser(viewersContainer, SWT.BORDER);
newFileViewer.setText("and here is the new file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
newFileViewer.setLayoutData(data);
I tried to keep that model while creating the 4th part, with the following code :
Composite c = new Composite(shell, SWT.BORDER);
c.setLayout(new GridLayout(1, false));
GridData l = new GridData(SWT.FILL, SWT.FILL, false, false);
c.setLayoutData(l);
TextMergeViewerCreator tmvc = new TextMergeViewerCreator();
TextMergeViewer tmv = null;
tmv = (TextMergeViewer) tmvc.createViewer(c, new CompareConfiguration());
DiffNode d = null;
try {
d = (DiffNode) (new CompareInput().prepareInput(new IProgressMonitor() {
//Some overrided methods
}
}));
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
d.setDontExpand(false);
tmv.setInput(d);
I guess that the DiffNode is not responsible on the TextMergeViewer file, but I cannot find what is wrong in my code.
Any help would be appreciated !
Thanks for reading.
You need to set a GridData to the Control of TextMergeViewer:
tmv.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Also, as a note: Do not reuse GridData objects. Each widget should have its own GridData.
Reference:
NOTE: Do not reuse GridData objects. Every control in a Composite that is managed by a GridLayout must have a unique GridData object.
I cannot figure out how to correctly set the height of my row composite. I would like the following code snippet to display only one row of data. Any additional rows would be seen by using the scrollbar. I have tried using rowContainer.setSize() but to no avail. Any help would be appreciated.
Please note that the scrolled composite is not directly linked to the rowContainer composite and contains other composites as well.
private void doStuff(final Composite parentContainer) {
final ScrolledComposite scrolledComposite = new ScrolledComposite(
parentContainer, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
final Composite borderComposite = new Composite(scrolledComposite,
SWT.BORDER);
borderComposite.setLayout(new GridLayout(1, false));
borderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
scrolledComposite.setContent(borderComposite);
scrolledComposite.setSize(borderComposite.computeSize(SWT.DEFAULT,
SWT.DEFAULT));
// Create a container for a label and button
final Composite labelAndAddButtonContainer = new Composite(
borderComposite, SWT.NONE);
labelAndAddButtonContainer.setLayout(new GridLayout(2, false));
labelAndAddButtonContainer.setLayoutData(new GridData(SWT.LEFT,
SWT.TOP, false, false));
// Create a container to hold all dynamic rows
final Composite rowContainer = new Composite(borderComposite, SWT.NONE);
rowContainer.setLayout(new GridLayout(1, false));
rowContainer.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
// Create at least one row.
final Collection<String> rowData = getRowData();
if (rowData.isEmpty()) {
createRow(scrolledComposite, rowContainer, "");
} else {
for (final String text : rowData) {
createRow(scrolledComposite, rowContainer, text);
}
// How do I adjust for the height so that only 1 row shows,
// and scrollbars appear if there is more than one row?
}
// Create a button
final Button addButton = new Button(labelAndAddButtonContainer,
SWT.PUSH);
addButton.setImage(someImage);
final Label label = new Label(labelAndAddButtonContainer, SWT.NONE);
label.setText("SOME_LABEL");
}
I have a JFace dialog which contains a Label , Text and Button side by side respectively.
I have set the initial size of the dialog and in my machine i have the dialog in the way i wanted.
But on another machine i only partially see the button.
This is how i have overriden the createDialogArea method.
#Override
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
GridLayout gridLayout = (GridLayout) container.getLayout();
gridLayout.numColumns = 3;
new Label(container, SWT.NONE);
new Label(container, SWT.NONE);
new Label(container, SWT.NONE);
Label fileLabel = new Label(container, SWT.NONE);
fileLabel.setText("File Name :");
fileText = new Text(container, SWT.BORDER);
GridData fileTextGD = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
fileTextGD.widthHint = 319;
fileText.setLayoutData(fileTextGD);
fileText.setEnabled(false);
browseButton = new Button(container, SWT.NONE);
GridData browseButtonGD = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
browseButtonGD.widthHint = 28;
browseButton.setLayoutData(browseButtonGD);
browseButton.setText("...");
return container;
}
What would be the problem?
Please suggest
The widthHint values you are using are probably too small for the font used on the second machine. Do not use widthHint, let the GridLayout work out the sizes for you.