Friday, February 13, 2009

Eclipse : Adding a Static text to the toolbar

There are situations you need to show a static text on the toolbar with a user name or some sort of text. This can be done using the ControlContribution.

JFace provides ControlContribution which can be added to the toolbar.




public class LabelControlContribution extends ControlContribution
{

protected LabelControlContribution(String id)
{
super(id);
}

@Override
protected Control createControl(Composite parent)
{
final Label b = new Label(parent, SWT.LEFT);
b.setText("Label: <Your User Name>");
return b;
}

}



Add the ControlContribution to the coolbar in the ApplicationActionBarAdvisors fillCoolbar method.


IToolBarManager toolbar1 = new ToolBarManager(SWT.FLAT
| SWT.RIGHT_TO_LEFT);
coolBar.add(new ToolBarContributionItem(toolbar1, "label"));
toolbar1.add(new LabelContribution("Label"));


Now the toolbar with the ControlContribution gets added to the Toolbar.

No comments: