Reputation: 1800
I am trying to create a container which will contain a buttons in it after certain amount of buttons I want to get a scroll bar but at the moment scroll bar is not appearing any suggestions?
EDIT
MoBuConLTLUI Class
public class MoBuConLTLUI extends Composite {
public MoBuConLTLUI(Composite parent) {
super(parent, SWT.NONE);
this.setLayout(new GridLayout());
createScrollableComposite(parent);
}
public void createScrollableComposite (Composite parent) {
// set the size of the scrolled content - method 1
final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
final Composite c1 = new Composite(sc1, SWT.V_SCROLL);
sc1.setContent(c1);
sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
GridLayout layout = new GridLayout();
layout.numColumns = 1;
c1.setLayout(layout);
Button b1 = new Button (c1, SWT.PUSH);
b1.setText("first button");
c1.setSize(200,200);
Button add = new Button (parent, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Button button = new Button(c1, SWT.PUSH);
button.setText("button "+index[0]);
// reset size of content so children can be seen - method 1
c1.layout();
}
});
}
}
EDIT The parent is initialized like following
public class TestBundleUI extends AbstractEntryPoint{
private static final long serialVersionUID = -7954149221017272321L;
private Composite testUiParentComposite;
@Override
public void createContents(Composite parent) {
this.testUiParentComposite = parent;
testUiParentComposite.setLayout(new GridLayout());
new MoBuConLTLUI(parent);
}
}
Upvotes: 1
Views: 2235
Reputation: 594
Try to change listener implementation.
scrolledComposite.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
scrolledComposite.setMinSize(cs1.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));
}
});
Upvotes: 0
Reputation: 36894
There are three things missing from your code:
Your Composite
shouldn't use SWT.V_SCROLL
as the style. Just use SWT.NONE
.
You should call the following on the ScrolledComposite
:
sc1.setExpandHorizontal(true);
sc1.setExpandVertical(true);
Update the min size in the listener:
c1.layout();
sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Upvotes: 3
Reputation: 111142
The main thing is that you are missing a
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
in the selection listener.
Also you should use SWT.NONE
for the c1
Composite
, using SWT.V_SCROLL
gives an unwanted extra scroll bar.
Since you are not using setExpandVertical
there is no point in called setMinSize
.
So this works:
final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
final Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
c1.setLayout(layout);
final Button b1 = new Button (c1, SWT.PUSH);
b1.setText("first button");
c1.setSize(200,200);
final Button add = new Button(parent, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event e) {
index[0]++;
final Button button = new Button(c1, SWT.PUSH);
button.setText("button "+index[0]);
// reset size of content so children can be seen - method 1
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c1.layout();
}
});
Upvotes: 3