Wednesday, August 31, 2011

Adding Control Decoration to SWT Controls

Adding Control Decoration to SWT Controls 

1.Create a SWT Control. For e.g. Text
2.Create a ControlDecoration Object.
It takes 2 Parameters.
a) Control for which this has to act as a Decoration
b) Position where the Decoration has to appear.
3.On the ControlDecoration object, assign an Image to show as Decoration using .setImage method.
4.On the ControlDecoration object, call the hide method as it shouldnt be shown by default.
5.Whenever you want to show the Decoration, call the show method on the ControlDecoration Object.
6.Whenever you want to hide the Decoration, call the hide method on the ControlDecoration Object.
Code Snippet ::
final Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
text.setText("");
final ControlDecoration dec = new ControlDecoration(text, SWT.TOP | SWT.LEFT);
dec.setImage(PlatformUI.getWorkbench().
getSharedImages().getImage(ISharedImages.IMG_DEC_FIELD_ERROR));
dec.setShowOnlyOnFocus(true);
dec.hide();
text.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
if (text.getText().length() > 0) {
dec.hide();
} else {
dec.show();
}
}
});

1 comments:

Mark said...

... and if you use databinding look at the ControlDecorationSupport.

Mark