Saturday, August 9, 2008

Communication between Views in the Eclipse IDE

One of the head-scratchers that come up after a bit of programming for Eclipse using SWT or programming your own app using Eclipse as a backbone is that you have to communicate between views. I recently bumped into this while developing my own software, so I thought I'd share how it's done very simply without singletons or any other gimmicks.

public class InfoView extends ViewPart {
private TransactionsView transactionsView;
public void createPartControl(Composite parent) {
private IPartListener partListener = new IPartListener() {
public void partOpened(IWorkbenchPart part) {
if (part instanceof TransactionsView) {
transactionsView = (TransactionsView)part;
private void myOwnFunction() {
if (transactionsView != null) {

That's all there is to it. When your view is created in createPartControl() tell Eclipse to add a listener that gets notified whenever a part-related event occurs and you'll know when the TransactionsView is opened. Some other part events you can listen to are:

public void partClosed(IWorkbenchPart part){}
public void partActivated(IWorkbenchPart part) {}
public void partBroughtToTop(IWorkbenchPart part) {}
public void partDeactivated(IWorkbenchPart part) {}

You'll have to make sure InfoView gets added to the perspective before TransactionsView otherwise the TransactionsView will be created before you add the listener and you won't get the notification.

Keywords for beloved Google:
eclipse swt
communicate betweeen views
call functions in another view
reference to another view
access another view's members
access another view's variables


Anonymous said...

Quite simple, just doing what i was looking for... thanks !

Anonymous said...

Thanks a lot. As the anonymous person above said, exactly what i wanted to know.