Saturday, May 22, 2010

Add a Toolbar to a View in an Eclipse RCP Application

This article shows how to add a toolbar to a View and builds off of a clean RCP Application with a View. The full source code is found at the end of the article. Eclipse RCP applications such as the Eclipse IDE, can have a drop down menu and/or, as demonstrated here, a toolbar located at the top of any View. With just a few lines of code you can easily add an icon to a View's toolbar and have it activate an Action when clicked.

Step 0: Create a RCP Application with a View.

Step 1: Create an Action that defines what will happen when the icon in the toolbar is clicked. Make a new class that extends org.eclipse.jface.action.Action and implements IWorkbenchAction. The class needs a private static final String property used to set the ID in the constructor. Inside the run() method is where you put your custom code. In this "CustomAction" example, code is added to the run() method that opens up a JFace MessageDialog box using a handy static convenience method of the MessageDialog class.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

public class CustomAction extends Action implements IWorkbenchAction{

private static final String ID = "com.timmolter.helloWorld.CustomAction";

public CustomAction(){

public void run() {

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
String dialogBoxTitle = "Message";
String message = "You clicked something!";
MessageDialog.openInformation(shell, dialogBoxTitle, message);


public void dispose() {}


Step 2:Add the toolbar with its Action and icon to the View. In the View's createPartControl method new-up the Action, and set the Action's tool-tip text and icon. In this example, a .png icon image called "bomb.png" was added to the icons folder in the plugin project. To find out where to find nice icons for Eclipse RCP projects see: Add an Icon to an Eclipse RCP Application View. Add the Action to the View's ToolBarManager using the getViewSite().getActionBars().getToolBarManager().add(lCustomAction) method.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

public static final String ID = "com.eclipsercptutorials.addViewToolBar.MainView"; // the ID needs to match the id set in the view's properties

public MainView() { }

public void createPartControl(Composite parent) {

// Custom Action for the View's Menu
CustomAction lCustomAction = new CustomAction();
lCustomAction.setText("Open Dialog Box");


public void setFocus() { }


Step 3: Run the application and test if everything worked. Your application should now have a View with an icon in the View's toolbar, which opens up a MessageDialog when clicked. If you're not seeing it, nor the View's tab and title for that matter, make sure you specify that the title of the View should be shown in the layout.addStandaloneView() method of the Perspective's createInitialLayout() method - the second argument of the addStandaloneView() should be set to true.
package com.eclipsercptutorials.addviewtoolbar;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {

layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f, layout.getEditorArea());
layout.setEditorAreaVisible(false); //hide the editor in the perspective


Piece of Cake!!

<--- Previous - Animation in Eclipse RCP Applications - A Bouncing Ball
---> Next - Add a Menu to a View in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents


Anonymous said...


can I do it directly by using the plugin.xml file ?


Tim Molter said...

Anonymous, probably, but I'm not sure how...

SoboLAN said...

This was exactly what I was looking for. And it worked. Many thanks ! :)