It is straight forward to add buttons into the toolbars. But, it can be a bit tricky to add other components, such as combo boxes. Again just adding the controls is not enough, because you will have to handle the events to make them useful. To add combo boxes or any other controls, one easy way is to create a custom control.

In this article, I will show a way to add combo boxes to toolbars by creating a custom control. With this approach, you can put just anything in the toolbars.

Lets take a look at following code.

import org.eclipse.jface.action.ControlContribution;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class MyCombo extends ControlContribution {

	Combo combo;
	public MyCombo(String str)
	{
		super(str);
	}

	@Override
	protected Control createControl(Composite parent)
	{
		combo = new Combo(parent, SWT.NONE | SWT.DROP_DOWN | SWT.READ_ONLY);

		combo.add("String 1");
		combo.add("String 2");
		combo.add("String 3");
		combo.add("String 4");
		combo.setTextLimit(10);
		combo.select(0);

		combo.addModifyListener(
		new ModifyListener()
		{
			public void modifyText(final ModifyEvent e)
			{
		MessageDialog.openInformation(
				null, "My App",
				"Item at " + combo.getSelectionIndex() + " clicked.");

			}
		});

		return combo;
	}

	public void setValue(int index)
	{
		combo.select(index);
	}

}

This code creates a custom control called MyCombo. This custom control is extended from ControlContribution.

The components to be contained within this custom control and their layouts are defined in the createControl method. In our case, we just need to show a combo box. In addition to instantiating the combo box, I have put some additional codes to handle the events also.

It might be a good idea to put a method that can be called externally to manipulate the items in combo box. So, just for a demo purpose, I also put a method called setValue.

Now, to add this custom control go to your application’s ActionBarAdvisor derived class. Usually in a normal RCP application, the name of this derived class is ApplicationActionBarAdvisor. Here you will have to override a method. A sample is shown below.


    MyCombo myCombo = null;

    protected void fillCoolBar(ICoolBarManager coolBar)
    {
    	IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
    	coolBar.add(toolbar);

    	myCombo = new MyCombo("Demo Combo box");
	toolbar.add(myCombo);
    }

Now, if everything goes right then the combo box should appear on the toolbar.



One Response to “How to put combo-boxes into toolbars in Eclipse RCP/Plugin ?”  

  1. Hi!

    Very good article, it really helped me to add some custom components to coolbar. I have some question – I have tried to add table to CoolBar in the same way – table was added but actually coolbar didn’t increase its height to fit the table size. How can I resize the CoolBar properly to place all required components there?

    Thanks!


Leave a Reply