You are here: Home > Knowledge Base > Software Engineering Blog > Eclipse - How To Make a Scrollable View?
 
Newsletter
Newsletter
(Required)
Log in


Forgot your password?
 

Eclipse - How To Make a Scrollable View?

Posted by Mirosław Ochodek at Apr 27, 2010 09:45 AM |
Filed under: ,

Sometimes you may like to have a scrollable view that contains another composite. The question is how to make it scrollable and adjustable to its content?

Eclipse - How To Make a Scrollable View?

SWT ScrolledComposite

Sometimes you may like to have a scrollable view that contains another composite. The question is how to make it scrollable and adjustable to its content?

There is a special class in SWT named ScrolledComposite which can be used for that purpose. In Figure 1 below you can see the example of a view (it is actually the view I needed it for).   

 scrollable view

Fig. 1 An example of scrollable view in Eclipse SWT

 

In my case there was a main composite named container, which contained a subContainer with all controls, i.e., lables, texts. To make the subContainer scrollable I introduced ScrolledComposite in the following way:

container = new Composite(parent, SWT.NONE);
container.setLayout(new FillLayout());

final ScrolledComposite sc = new ScrolledComposite(container, SWT.BORDER | SWT.H_SCROLL 
                                                   | SWT.V_SCROLL);
sc.setLayout(new FillLayout());
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);

final Composite subContainer = new Composite(sc, SWT.NONE);
GridLayout layout = new GridLayout();

sc.setContent(subContainer);

The scrolled composite was added directly to the container, then, the subContainer was added to the scrolled composite. You have to also set the subContainer as a content of the scrolled composite.

The make scrolled composite aware of changes in size of the subContainer you can add listener:

sc.addControlListener(new ControlAdapter() {
    public void controlResized(ControlEvent e) {
        Rectangle r = sc.getClientArea();
        sc.setMinSize(subContainer
                .computeSize(r.width, SWT.DEFAULT));
    }
});

 

Document Actions
Supporting only the best, so that they can become even better