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.
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'm trying to put a ScrolledComposite on my UI with SWT.
but now it only shows blank area or something wrong.
The area to show ScrolledComposite is blank without the code,
"optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT)); "
This is the pic of screenshot.
The optionTab should have the buttons with Scroll....
and with the code, it shows scroll, but too long height and narrow width.
Somehow, the upper is gone, but come back when I extend the size of window with scroll disappearing.
I set the color of background red to make it easier to see for you.
Can anyone help me make "optionCompositeAtLeft" filled in ScrolledComposite?
I want the left area of Option tab to be filled with a red background from "optionCompositeAtLef".
Below is my code.
private BomCloneDialog dialog = null;
private TabFolder tabFolder = null;
private TabItem tabOption = null;
public CreateOptionTab(TabFolder tabFolder, TabItem tabOption) {
this.tabFolder = tabFolder;
this.tabOption = tabOption;
}
public void createOptionTabUI() {
GridData gdWithFillBoth = new GridData(GridData.FILL_BOTH);
Composite optionComposite = new Composite(tabFolder, SWT.NONE);
optionComposite.setLayout(new GridLayout(2, true));
tabOption.setControl(optionComposite);
ScrolledComposite scrolledCompositeForOption = new ScrolledComposite(optionComposite,SWT.V_SCROLL | SWT.BORDER);
scrolledCompositeForOption.setLayoutData(gdWithFillBoth);
Composite optionCompositeAtLeft = new Composite(scrolledCompositeForOption, SWT.BORDER);
scrolledCompositeForOption.setContent(optionCompositeAtLeft);
optionCompositeAtLeft.setLayout(new GridLayout(1, true));
optionCompositeAtLeft.setLayoutData(gdWithFillBoth);
optionCompositeAtLeft.setBackground(new Color(Display.getCurrent(), 255,0,0));
Button b [] = new Button[30];
for(int a=0; a<30; a++){
b[a] = new Button(optionCompositeAtLeft, SWT.PUSH);
b[a].setText("button"+a);
}
**optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT));**
Composite optionButtonComposite = new Composite(optionComposite, SWT.BORDER);
optionButtonComposite.setLayout(new GridLayout(1, true));
optionButtonComposite.setLayoutData(gdWithFillBoth);
Button optionSaveButton = new Button(optionButtonComposite, SWT.NONE);
optionSaveButton.setLayoutData(gdWithFillBoth);
optionSaveButton.setText("Save");
Button optionAddButton = new Button(optionButtonComposite, SWT.NONE);
optionAddButton.setLayoutData(gdWithFillBoth);
optionAddButton.setText("Add");
}
You need to configure ScrolledComposite to resize the content:
scrolledCompositeForOption.setExpandHorizontal(true);
scrolledCompositeForOption.setExpandVertical(true);
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.
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");
}
When i create a check button and push button on both ends of the dialog in a buttons bar,
it is defaultly created well, but when i drag or resize the dialog, the check button moves away from the left end of the dialog. How should I prevent it from moving when i resize the dialog.
This is how i overrided my buttons bar.
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
((GridLayout) parent.getLayout()).numColumns = ((GridLayout) parent.getLayout()).numColumns+2;
GridData checkData = new GridData(SWT.LEFT,
SWT.BOTTOM,true,true);
GridData labelData = new GridData(SWT.CENTER,
SWT.CENTER,true,true);
GridData closeLayoutData = new GridData(SWT.RIGHT,
SWT.BOTTOM,true,true);
parent.setLayoutData(closeLayoutData);
Button close = getButton(IDialogConstants.CANCEL_ID);
close.setText("Close");
close.setLayoutData(closeLayoutData);
close.setParent(parent);
final Button checkButton = new Button(parent, SWT.CHECK);
checkButton.setText("View All schedules");
checkButton.setSelection(false);
checkButton.setLayoutData(checkData);
checkButton.setParent(parent);
checkButton.moveAbove(close);
checkButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (checkButton.getSelection()) {
treeViewer.setFilters(new ViewerFilter[] { new ExistingExpiredScheduleFilter() });
} else {
treeViewer.setFilters(new ViewerFilter[] { new ExistingScheduleFilter() });
}
}
});
Label label = new Label(parent,SWT.HORIZONTAL);
label.setLayoutData(labelData);
label.setParent(parent);
label.moveAbove(close);
}
This works:
protected void createButtonsForButtonBar(Composite parent)
{
final GridLayout layout = (GridLayout)parent.getLayout();
layout.numColumns += 2;
layout.makeColumnsEqualWidth = false;
parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
final Button checkButton = new Button(parent, SWT.CHECK);
checkButton.setText("View All schedules");
checkButton.setSelection(false);
checkButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
// Left out selection code for clarity
final Label label = new Label(parent, SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, "Close", false);
}
There is no need to call setParent. I have created things in order so there is no need to reorder.