SWT snippet: an image viewer with a scrolled composite

I don’t know why, but among all the SWT controls, ScrolledComposite is the most difficult for me. I mean, every time I have to use it, it does not work at the first try.

So, here is a snippet of my own. It completes the usual ones provided by SWT.
It consists in viewing an image in a dialog. If the image is bigger than a given size, then scroll bars appear.

package com.ebmwebsourcing.swt.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewSnippet {

	public static void main( String [] args ) {

		// Display + shell
		Display display = new Display ();
		Shell shell = new Shell( display );
		shell.setLayout( new GridLayout( 2, false ));

		// Load an image
		//ImageData imgData = new ImageData( "your image path" );
		ImageData imgData = new ImageData( "D:/Divers/wallpapers/1920-1200-74378.jpg" );
		Image image = new Image( display, imgData );

		// The scrolled composite
		ScrolledComposite sc = new ScrolledComposite( shell, SWT.H_SCROLL | SWT.V_SCROLL );
		GridData layoutData = new GridData( GridData.FILL_HORIZONTAL );
		layoutData.horizontalSpan = 2;
		layoutData.heightHint = 400;
		sc.setLayoutData( layoutData );

		Label imgLabel = new Label( sc, SWT.NONE );
		imgLabel.setImage( image );
		imgLabel.setSize( imgLabel.computeSize( SWT.DEFAULT, SWT.DEFAULT ));
		sc.setContent( imgLabel );

		Label l = new Label( shell, SWT.NONE );
		l.setText( "Nom actuel :" );
		layoutData = new GridData();
		layoutData.verticalIndent = 20;
		l.setLayoutData( layoutData );

		Text text = new Text( shell, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY );
		text.setText( "The image name" );
		layoutData = new GridData( GridData.FILL_HORIZONTAL );
		layoutData.verticalIndent = 20;
		text.setLayoutData( layoutData );

		// Run it
		shell.setSize( 800, 600 );
		while( ! shell.isDisposed()) {
			if( ! display.readAndDispatch())


And here are two snapshots of the result.

An image viewer that scrolls when needed
An image viewer that scrolls when needed
An image viewer that scrolls when needed (scrolled view)
An image viewer that scrolls when needed (scrolled view)

One thought on “SWT snippet: an image viewer with a scrolled composite

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s