ScrolledComposite scrollFormItemComposite = new ScrolledComposite(c, SWT.V_SCROLL | SWT.BORDER);
GridData formItemCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
formItemCompositeGridData.horizontalSpan = 4;
scrollFormItemComposite.setLayoutData(formItemCompositeGridData);
GridLayout formItemLayout = new GridLayout(1, true);
formItemLayout.marginHeight = 0;
formItemLayout.marginWidth = 0;
formItemLayout.verticalSpacing = 0;
formItemLayout.horizontalSpacing = 0;
scrollFormItemComposite.setLayout(formItemLayout);
Composite formItemComposite = new Composite(scrollFormItemComposite, SWT.RESIZE);
formItemComposite.setLayout(new FillLayout(SWT.VERTICAL|SWT.HORIZONTAL));
FormProvider formProvider = new FormProvider();
formProvider.createForms(formItemComposite);
scrollFormItemComposite.setContent(formItemComposite);
scrollFormItemComposite.setExpandHorizontal(true);
scrollFormItemComposite.setExpandVertical(true);
scrollFormItemComposite.setMinSize(formItemComposite.computeSize(300,SWT.DEFAULT));
The previous lines of code give me the following output. I want the contents to shrink as the form becomes smaller. I dont want the horizontal scroll bar to appear. How do I prevent the text box from being hidden?
Related
I am trying the following code to set scrollcomposite for TabItem "item2". But couldn't get the scroll bar.
Here I created two tabItem , where I need to set scrollcomposite / scrollbar for only item2 not for item1
Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
// First Tab Item
TabItem item = new TabItem(tabFolder, SWT.NONE);
item.setText("TabItem " + 1);
Composite comp = new Composite(tabFolder, SWT.NONE);
GridLayout gl = new GridLayout();
GridData wgd = new GridData(GridData.FILL_BOTH);
comp.setLayout(gl);
comp.setLayoutData(wgd);
Button button = new Button(comp, SWT.PUSH);
button.setText("Page " + 1);
Button button2 = new Button(comp, SWT.PUSH);
button2.setText("Page " + 1);
Button button3 = new Button(comp, SWT.PUSH);
button3.setText("Page " + 1);
Button button4 = new Button(comp, SWT.PUSH);
button4.setText("Page " + 1);
item.setControl(comp);
ScrolledComposite sc = new ScrolledComposite(tabFolder, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
// second tab item
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
item2.setText("TabItem " + 1);
Composite comp2 = new Composite(tabFolder, SWT.NONE);
GridLayout gl2 = new GridLayout();
GridData wgd2 = new GridData(GridData.FILL_BOTH);
comp2.setLayout(gl2);
comp2.setLayoutData(wgd2);
Button buttonq = new Button(comp2, SWT.PUSH);
buttonq.setText("Page " + 1);
Button button2q = new Button(comp2, SWT.PUSH);
button2q.setText("Page " + 1);
sc.setContent(comp2);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(comp2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
item2.setControl(comp2);
tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
When I added following code, tabItem2 was empty:
item2.setControl(comp2);
Please help me to solve this
Several things here.
First use layouts for everything. The tabFolder.setLocation is causing confusion, use FillLayout instead.
So replace
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
with
shell.setLayout(new FillLayout());
Second, the Composite for the second tab must be owned by the ScrolledComposite.
So change
Composite comp2 = new Composite(tabFolder, SWT.NONE);
to
Composite comp2 = new Composite(sc, SWT.NONE);
Finally the ScrolledComposite must be the control for the second tab, so change
item2.setControl(comp2);
to
item2.setControl(sc);
Below code is working fine if i am using shell. but i am using the composite its not working.
code working fine if we use the shell:
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
same code is not working if we use the composite in gridlayout:
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
Composite c1 = new Composite(shell, SWT.NONE);
c1.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(c1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
Your aren't telling your c1 composite how it should behave in relation to its parent (i.e. set a layout data).
You have two options:
c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
OR
shell.setLayout(new FillLayout());
I am trying to create a view with two trees that take up the entire display. As of now, the two trees are only taking up half (left half) of the canvas. I cannot figure out why, as I have tried many different parameters sent to each of the setup methods, such as setLayout, SashForm, etc. Here is my code and I attached an image to show what a simplified view of what I am getting now is.
super(parent);
setDisplayName("Viewer");
setLayout(new FillLayout());
((FillLayout) getLayout()).marginHeight = ((FillLayout) getLayout()).marginWidth = 20;
fullForm = new SashForm(this, SWT.VERTICAL);
fullForm.setLayout(new FillLayout());
adForm = new SashForm(fullForm, SWT.VERTICAL);
adForm.setLayout(new FillLayout());
countLabel = GUIToolkit.newLabel(this, SWT.CENTER, "", new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
countLabel.setFont(boldFont);
ad1Tree = createTree(fullForm, "name1", "col1", "col2");
ad2Tree = createTree(fullForm, "name2", "col1", "col2");
Create Tree
private static Tree createTree(final Composite parent, final String text, final String... columns) {
final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, null);
group.setFont(FontManager.NORMAL_BOLD);
group.setLayout(new FillLayout());
final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
tree.setHeaderVisible(true);
GUIToolkit.createColumns(tree, columns);
GUIToolkit.addColumnSort(tree, DATA);
GUIToolkit.removeDoubleClickExpand(tree);
I think your problem is that you don't assign a GridData to the Group containing the Tree and the Tree itself.
Try updating your code like this:
private static Tree createTree(final Composite parent, final String text, final String... columns) {
final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
group.setFont(FontManager.NORMAL_BOLD);
group.setLayout(new FillLayout());
final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
tree.setHeaderVisible(true);
GUIToolkit.createColumns(tree, columns);
GUIToolkit.addColumnSort(tree, DATA);
GUIToolkit.removeDoubleClickExpand(tree);
}
Not sure what exactly GUIToolkit is, but I'm assuming it will just apply the LayoutData you provide.
UPDATE:
Ok, this is how I would do it:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Group top = new Group(shell, SWT.NONE);
top.setLayout(new GridLayout(1, false));
top.setText("Top");
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Group bottom = new Group(shell, SWT.NONE);
bottom.setLayout(new GridLayout(1, false));
bottom.setText("Bottom");
bottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Tree topTree = new Tree(top, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
createColumns(topTree);
Tree bottomTree = new Tree(bottom, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
createColumns(bottomTree);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static void createColumns(Tree tree)
{
tree.setHeaderVisible(true);
for(int i = 0; i < 3; i++)
new TreeColumn(tree, SWT.NONE).setText("Col " + i);
for(int i = 0; i < 10; i++)
{
TreeItem item = new TreeItem(tree, SWT.NONE);
for(int j = 0; j < tree.getColumnCount(); j++)
{
item.setText(j, "Item " + i + " " + j);
}
}
for(int i = 0; i < tree.getColumnCount(); i++)
tree.getColumn(i).pack();
}
I'm trying to display a label in a 2-columned GridLayout. I can't seem to make it work to display both text and the horizontal line underneath:
public void foo(){
popupShell = new Shell(Display.getDefault(), SWT.NO_TRIM | SWT.ON_TOP | SWT.MODELESS);
//popupShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
popupShell.setLayout(createNoMarginLayout(2, false));
Label history = new Label(popupShell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
history.setText("History");
history.setVisible(true);
Label fill = new Label(popupShell, SWT.NONE);
fill.setSize(0,30);
}
public static GridLayout createNoMarginLayout(int numColumns, boolean makeColumnsEqualWidth) {
GridLayout layout = new GridLayout(numColumns, makeColumnsEqualWidth);
layout.verticalSpacing = 0;
layout.horizontalSpacing = 0;
layout.marginTop = 0;
layout.marginBottom = 0;
layout.marginLeft = 0;
layout.marginRight = 0;
layout.marginWidth = 0;
layout.marginHeight = 0;
return layout;
}
What I'm getting is just the line with no text.
What am I doing wrong?
A Label with the SWT.SEPARATOR style does not display any text value. You must use a separate control to display the text.
From the Label source, showing that setText is completely ignored for SWT.SEPARATOR:
public void setText (String string) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
if ((style & SWT.SEPARATOR) != 0) return;
...
How can I position a Button (vertically) at the center using a FormLayout (see here)? Using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
layoutData.top = new FormAttachment(50);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);
I end up with
Which is not surprising, since I told it to put the top of the button at the center (layoutData.top = new FormAttachment(50);). How can I instead put the center of the button at the center?
You can specify an offset with the constructor:
new FormAttachment(int numerator, int offset)
Looks like this:
You can compute the offset using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
/* Compute the offset */
int offset = -button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y / 2;
/* Create the FormAttachment */
layoutData.top = new FormAttachment(50, offset);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);