Sunday, June 22, 2008

Gradient Background to any SWT Control

Helper class that add gradent bg to a give control

public class GradientHelper{

private static Image oldImage = null;

public static void applyGradientBG(
Composite composite) {
Rectangle rect = composite.getClientArea();
Image newImage = new Image(composite.getDisplay(), 1, Math.max(1,
rect.height));
GC gc = new GC(newImage);
gc
.setForeground(composite.getDisplay().getSystemColor(
SWT.COLOR_WHITE));
gc.setBackground(new Color(composite.getDisplay(), 228, 234, 243));
gc.fillGradientRectangle(0, 0, 1, rect.height, true);
gc.dispose();
composite.setBackgroundImage(newImage);

if (oldImage != null)
oldImage.dispose();
oldImage = newImage;
}
}


Examples

Adding background to a tree.

treeView.getTree().
addListener(SWT.Resize,new Listener() {
public void handleEvent(Event event) {
GradientHelper.applyGradientBG(rreeViewer.getTree());
}
});

2 comments:

Unknown said...

Thanks, very helpful.

Anonymous said...

Works like charm! Thankx a lot!