Present Large Chunks of Code with JFace TitleAreaDialog


TextAreaDialog example to show large code to user

Some times it is surprising how long you need to solve a very particular but in also very simple problem. I thought I better share the solution.

The Problem

Often when programming eclipse plug-ins, you want to show some piece of code to the user. Just for informational purposes or as debug info. For example an XML-representation of a model your software handles, or some log-dump, a textual representation for some graph you visualize, etc.

The Solution – TitleAreaDialog

In eclipse, you have JFace and SWT as GUI-programming APIs. JFace provides you with TitleAreaDialog, a class of dialogs that gives you a “title area” showing a label that describes what the user sees and a pair ok/cancel buttons. In between the title area and the two buttons you can place whatever you want. We want to place a text area with the code that we want to show to the user.

Here it is:

public class ShowCodeDialog extends TitleAreaDialog {
	private Text text;
	private final String code;
	public ShowCodeDialog(Shell parentShell, String code) {
		this.code = code;
	public void create() {
		setMessage("This explains what you see.",
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		text = new Text(parent, SWT.MULTI | SWT.BORDER |
                        SWT.H_SCROLL | SWT.V_SCROLL);
		return parent;
ShowCodeDialog codeDialog = new ShowCodeDialog(
        shellFromSomeWhere, "...code...");

Should be self explanatory.

Leave a Reply

Required fields are marked *.