Welcome to our active community of Eclipse RCP developers! Visit the tutorials Table of Contents or search for the topics you need help with. Then return the favor by contributing your own tip or trick. Don't hesitate, get sharing right away. It's easy!

Thursday, May 28, 2009

Using Simple JFace Message Boxes in an Eclipse RCP Application

This article shows how to use JFace messages boxes in an RCP application and builds off of a clean RCP Application with a View. The full source code is found at the end of the article. Most if not all GUI applications frequently use message boxes to give or get information to or from the user. Jface provides us with 5 commonly used message boxes which can be conveniently used via a single static method. The available message boxes are:

  • MessageDialog.openQuestion()
  • MessageDialog.openInformation()
  • MessageDialog.openWarning()
  • MessageDialog.openConfirm()
  • MessageDialog.openError()
Each method takes as arguments a Shell that the message box belongs to, the title of the message box, and the message to be displayed in the message box. They each display an icon such as a question mark or a exclamation mark, etc. (For some reason the openQuestion message box displayes an exclamation point on a Mac?!?) and a button or buttons for the user to close the box or respond to a question. The openQuestion and openConfirm message boxes return a boolean corresponding to the users selection.

To demonstrate the JFace Message Boxes used in an Eclipse RCP application, a view containing a single button is created. When the button is clicked, a Question message box pops up, and depending on the boolean value returned, opens up either a Warning or an Information message box.

Step 0: Create a Hello World Eclipse RCP Application and add a View to it.

Step 1: Add code to the createPartControl() method in the application's view to add a button and set its action listener, coding the calls to the JFace message box static methods.


package com.eclipsercptutorials.jfacemessageboxes;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

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

public MainView() {}

public void createPartControl(Composite parent) {

Composite lMainViewComposite = new Composite(parent, SWT.NONE);

Button lButton = new Button(lMainViewComposite, SWT.PUSH);
lButton.setText("Question");
lButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){

Shell lShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

boolean isHavingFun = MessageDialog.openQuestion(lShell, "Information", "Are you having fun?");

if(isHavingFun){

MessageDialog.openInformation(lShell, "Information", "You are having fun! Java is awesome.");

}else{

MessageDialog.openWarning(lShell, "Warning", "You are NOT having fun! Go for a walk and get some fresh air.");

}
}
});

lMainViewComposite.setLayout(new RowLayout()); // this sets a Layout for the view's Composite

}

public void setFocus() {}

}


Step 2. Run the application and test if everything worked. Your application should now have a view with a single button that triggers a JFace question message box to pop up, and depending on the boolean value returned, opens up either a Warning or an Information message box:



Piece of Cake!!

Get the code: com.eclipsercptutorials.JFaceMessageBoxes.zip
<--- Previous - Add a Menu to a View in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents


If you enjoyed this post, make sure you subscribe to the Eclipse RCP Tutorials RSS feed to receive new posts in a reader or via email!

0 comments:

Post a Comment

Wanted: your Eclipse RCP tips and tricks. We created this website as a place to give back to the Eclipse open source community by offering people help in developing their own Rich Client Platform (RCP) applications. We hope you find value in the tutorials found here. Visit the share page for information on how to contribute a unique RCP Application tutorial or code snippet and give back to the community! The style of the tutorials are designed to be quick and easy to create by contributors and to understand by the people learning from them. Import the RCP projects found at the end of the tutorials and export your contributed projects as shown in Import and Export an Eclipse RCP Application Project.


Enter your email address to automatically
receive the newest articles:

 

Eclipse RCP Tutorials - beta Copyright © 2009