![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuLMXeuMR9FYIStwNRUn6sYBTGFmx8EVwD_tq64faTYmNojt2fajKirN9yWlGFRlltcdopfaiqpRpBHXuKJx6AcHvbmsRExXVZQwDnrTHoKKb87a-efGfyGnxeifwidVH5vUgrvNpo1Bb_/s200/eclipse+rcp+JFace+message+2.png)
- MessageDialog.openQuestion()
- MessageDialog.openInformation()
- MessageDialog.openWarning()
- MessageDialog.openConfirm()
- MessageDialog.openError()
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 an Eclipse RCP Application with a View.
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:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN4I0xTbRtT6czAlXqkCwPOPFup9eiwuWB-erH8DFUoQcOkRdXRR2MZH6x-veI1V5K0BgWI9CDTvOiJcAmSlKp_fhigg1zgSZhlgacL-wfWKCSwHFol5qdPYKWL3cRdIWZUtH9efe3n5Ff/s400/eclipse+rcp+JFace+message+1.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEises60loVlxKoTEu3gNjYTYB9iS0yLztxdccuE5vb3fzTksWA3H4X_BsEyqjC1pW5o63c7dknzK02xXpwmGXQ8-m8DLYJQzQeU7riS0cgtmzQEtbssZOEq1ckPXwPsh0d65k6aQNv_XDuQ/s400/eclipse+rcp+JFace+message+2.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7vDvfNdRJIZYQW0uV_G-TvpnZaxlZ3bjq4YU5hKs3uPWsXIwBoieNm-Y9VfILHeAvnwxWzCZfd6QuciBik2Gfc7jetXEJOwo_6_FF4drmAIHiJ4b_MKAm7P-YMrSZGzRXCQwiX8E5Np6b/s400/eclipse+rcp+JFace+message+4.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhboWdpaC6jaEe29DefUEIihRiX2iHB1VSG8nk58NaPq2Y0enUR6v-_6k3eX09C0bTLzrn_9vUQyB8O-ZZozeVC0V7-RcWWLf_daQuaXeJ19bYfdlTuL-c3WcN9hVTQwXltq9m6UJYPs36N/s400/eclipse+rcp+JFace+message+3.png)
Piece of Cake!!
<--- Previous - Add a Menu to a View in an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents
No comments:
Post a Comment