How can I show Scroll on my UI, with JAVA, SWT? - java

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);

Related

Unable to redraw SWT Composite in a Wizard Page

I have a composite, which has 3 UI controls - SWT Group, SWT TabFolder, and a child composite that has a label and a link. In some use-cases the SWT Group is hidden, and I would like that the blank space is not seen instead the composite has to be redrawn and show only the SWT TabFolder and the child composite, without any space.
I'm using composite.layout(true), but still there is blank space of the SWT Group when it is made hidden.
Could someone help me solve this issue
TestComposite extends Composite{
private Label childlabel;
private Group group;
private TabFolder tabFolder;
private Button button;
TestComposite(Compossite parent){
super(parent, SWT.NONE);
setLayout(new GridLayout(1, true));
setFont(parent.getFont());
setBackground(parent.getBackground());
GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
createControl();
}
public void createControl() {
this.group = new Group(this, SWT.SHADOW_IN);
this.group.setSize(this.getDisplay().getActiveShell().getSize());
this.button = new Button(this.group, SWT.PUSH);
GridDataFactory.swtDefaults().applyTo(this.button);
this.tabFolder = new TabFolder(this, SWT.TOP);
GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true,
true).applyTo(this.tabFolder);
Composite childComposite = new Composite(this, SWT.NONE);
childComposite .setLayout(new GridLayout(2, false));
GridDataFactory.swtDefaults().grab(true, false).align(SWT.LEFT,
SWT.TOP).applyTo(childComposite );
this.childLabel = new Label(childComposite, SWT.NONE);
GridDataFactory.swtDefaults().grab(true, false).applyTo(childlabel);
setInput(abc);
enableVisibilityOfGroup(false);
}
public void enableVisibilityOfGroup(Boolean visible){
this.group.setVisible(visible);
// this shoudl redraw the UI and shown only tabFolder and label in the
// whole wizard page, but there is blank space in place of group
this.layout(true);
}
}
Set GridData on the Group
GridDataFactory.swtDefaults().applyTo(this.group);
Use GridData.exclude to exclude/include the group in the layout:
public void enableVisibilityOfGroup(boolean visible) {
this.group.setVisible(visible);
GridData gridData = (GridData)this.group.getLayoutData();
gridData.exclude = !visible;
this.layout(true);
}

Eclipse SWT ScrolledComposite refuses to scroll

I'm developing a plugin for eclipse and I'm struggling to use ScrolledComposite as well as making it take up the remaining space.
The parent layout is a 2-column Grid:
#Override
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(2, false));
// other views etc
}
The ScrolledComposite is created like so (slightly abbreviated):
private void fillScroll() {
if (scroll != null) {
scroll.dispose();
}
scroll = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
GridData gd = new GridData();
gd.horizontalSpan = 2;
scroll.setLayoutData(gd);
Composite innerContainer = new Composite(scroll, 0);
innerContainer.setLayout(new GridLayout(1, false));
// for loop that adds widgets to innerContainer
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setContent(innerContainer);
scroll.setSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
parent.layout(true);
}
The list of questions are the widgets added in the for loop and are contained by innerContainer.
Expectation: Since innerContainer is too large to fit in the parent, there should be scroll bars.
What actually happens: There are no scroll bars.
How do I fix this problem?
The GridData on the ScrolledComposite needs to have grabExcessVerticalSpace set to true. I suggest you to use the extended constructor in order to clearly define how it should be displayed, for example:
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
Option A: change scroll.setSize to scroll.setMinSize:
scroll.setMinSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
or, Option B: remove scroll.setExpandHorizontal and scroll.setExpandVertical and change scroll.setSize to innerContainer.setSize:
innerContainer.setSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));

how to set the size for the button in JAVA?

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.

Input form using FormLayout in swt

i am new to swt and im trying to create a layout in a window. Each texfield will have a label to the left of the textfield- however in some cases there may be two textfields per label, and maybe at a later date there will be radio buttons added. Is a formlayout the best way of doing this? It seems to me unnecessarily over-complicated. I dont have windowbuilder or a visual designer utility and am finding the FormAttachment method difficult to handle. Any advice appreciated. I have attached a screenshot of the basic gui design Im trying to create.
Unfortunately im not allowed upload images for the moment, as i a new user. Essentially the structure i am aiming for is like so:
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL CALENDAR CONTROL
OK | NOK
Here is a simple example that should explain how to span a widget across multiple columns of a GridLayout:
public class StackOverflow2
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(3, false));
Label firstLabel = new Label(shell, SWT.NONE);
firstLabel.setText("Label");
Text firstText = new Text(shell, SWT.BORDER);
addSpanData(firstText, 2);
Label secondLabel = new Label(shell, SWT.NONE);
secondLabel.setText("Label");
Text secondText = new Text(shell, SWT.BORDER);
addSpanData(secondText, 1);
Text thirdText = new Text(shell, SWT.BORDER);
addSpanData(thirdText, 1);
Label thirdLabel = new Label(shell, SWT.NONE);
thirdLabel.setText("Label");
Text fourthText = new Text(shell, SWT.BORDER);
addSpanData(fourthText, 2);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addSpanData(Control comp, int span)
{
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.horizontalSpan = span;
comp.setLayoutData(data);
}
}
This is how it looks:

How can we auto resize the size of components in SWT?

In my SWT application i have certain components inside the SWT shell.
Now how can i auto re-size this components according to the size of display window.
Display display = new Display();
Shell shell = new Shell(display);
Group outerGroup,lowerGroup;
Text text;
public test1() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns=1;
shell.setLayout(gridLayout);
outerGroup = new Group(shell, SWT.NONE);
GridData data = new GridData(1000,400);
data.verticalSpan = 2;
outerGroup.setLayoutData(data);
gridLayout = new GridLayout();
gridLayout.numColumns=2;
gridLayout.makeColumnsEqualWidth=true;
outerGroup.setLayout(gridLayout);
...
}
i.e when I decrease the size of window the components inside it should appear according to that.
That sounds suspiciously like you aren't using layouts.
The whole concept of layouts makes worrying about resizing needless. The layout will take care of the size of all of its components.
I would recommend to read the Eclipse article about layouts
Your code can easily be corrected. Don't set the size of individual components, the layout will determine their size. If you want the window to have a predefined size, set the shell's size:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Group outerGroup = new Group(shell, SWT.NONE);
// Tell the group to stretch in all directions
outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
outerGroup.setLayout(new GridLayout(2, true));
outerGroup.setText("Group");
Button left = new Button(outerGroup, SWT.PUSH);
left.setText("Left");
// Tell the button to stretch in all directions
left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button right = new Button(outerGroup, SWT.PUSH);
right.setText("Right");
// Tell the button to stretch in all directions
right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.setSize(1000,400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Before resizing:
After resizing:

Categories