Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Initial state/scrolling when Drawing on SWT Components
Initial state/scrolling when Drawing on SWT Components [message #530010] Wed, 28 April 2010 06:45
Dominik Stadler is currently offline Dominik StadlerFriend
Messages: 3
Registered: July 2009
Junior Member

I would like to create a SWT custom component which uses some drawing, like a border and possible other UI gimmicks.

The code example below is a cut down version and illustrates my approach, it uses a ScrolledComposite to display a list of "items", each of them surrounded by a custom-drawn border. However two things do not work:
* The initial display is without the drawings, resizing shows the border correctly
* After resizing, if you scroll down, the part that is scrolled into view does not contain the border

Has anybody a hint for me what is done incorrectly in my code or how I can get the two things right?

Thanks... Dominik.

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

 * Sample class that shows problems with initial drawing of the border and 
 * problems on scrolling
public class DrawingIssueTest {
    private static final int START_X = 5;
    private static final int START_Y = 0;
    private static final int ROUND_DELTA_X = 10;
    private static final int ROUND_DELTA_Y = 5;
    private static final int ROUNDING = 15;

	public static void main(String[] args) throws Exception {
		Display display = new Display();
		Shell shell = new Shell(display);

		shell.setLayout(new FillLayout());

		// use a scrolled composite to scroll the items if they do not fit
        final ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);

	    Canvas main = new Canvas(scrolledComposite, SWT.NULL);
        main.setLayout(new GridLayout(1, false));

        // draw 20 components underneath each other
        for(int i = 0;i < 20; i++) {
        	create(main, "headline");

        scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		shell.setSize(400, 300);;

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())

	 * put a label and draw a rounded rectangle on the provided canvas.
	 * @param main
	 * @param title
    private static void create(final Canvas main, String title) {
        final Canvas inner = new Canvas(main, SWT.NONE);

        inner.setLayout(new GridLayout(2, false));
        inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        final Listener redrawListener = new Listener() {
			public void handleEvent(Event event) {
				if(!inner.isDisposed()) {
			        Rectangle rect = inner.getClientArea();
					GC gc = new GC(inner);
			        // clear the area before painting to avoid having artefacts from before
			        Color fg = gc.getForeground();
			        gc.drawRoundRectangle(START_X,START_Y,rect.width-ROUND_DELTA_X,rect.height-ROUND_DELTA_Y, ROUNDING, ROUNDING);
		// only need to draw on the background if we use the colored-version of the dialog
		main.getShell().addListener(SWT.Resize, redrawListener);
		main.getShell().addListener(SWT.Activate, redrawListener);

        final Canvas innerText = new Canvas(inner, SWT.NONE);
        innerText.setLayout(new GridLayout(1, false));

		Label titleLabel = new Label(innerText, SWT.NONE);

Previous Topic:Is it possible to use combo with cascade behavior ?
Next Topic:Is there something like treeitem.showCheckBox (boolean)
Goto Forum:

Current Time: Sat Apr 29 06:04:49 GMT 2017

Powered by FUDForum. Page generated in 0.04214 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software