SWT - TitleAreaDialog

java, jface, swt

Solution

I haven't used `TitleAreaDialog` yet, but here is a simple `Dialog` I use myself. It should give you an idea about the inner workings of a dialog. It's basically just a dialog with some error message and a checkbox:

public class CheckboxDialog extends Dialog {

    private String message = "";
    private String checkboxMessage = "";
    private boolean checkValue;

    private Button checkButton;

    /* Constructor, set shell style and set block on open (rest of gui is blocked until closed) */
    public CheckboxDialog(Shell parentShell) {
        super(parentShell);
        setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
        setBlockOnOpen(true);
    }

    /* creates the content of the dialog */
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea(parent);

        /* set the layout for the content (gridlayout with 1 column)*/
        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 15;
        layout.marginWidth = 30;
        composite.setLayout(layout);

        /* add a label with some text */
        final Label content = new Label(composite, SWT.NONE);
        content.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
        content.setText(message);

        /* add a checkbox button */
        checkButton = new Button(composite, SWT.CHECK);
        checkButton.setText(checkboxMessage);
        checkButton.setSelection(true);
        checkButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        return composite;
    }

    /* create the dialog buttons (in this case, only an OK button) */
    protected void createButtonsForButtonBar(Composite parent)
    {
        createButton(parent, IDialogConstants.OK_ID, "OK", true);
    }

    /* configure the dialog's shell (set title) */
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Error");
    }

    /* this method is executed if the OK button is pressed */
    public void okPressed()
    {
        checkValue = checkButton.getSelection();
        close();
    }

    /* getter and setter methods */
    public void setMessage(String message) {
        this.message = message;
    }

    public void setCheckboxMessage(String checkboxMessage) {
        this.checkboxMessage = checkboxMessage;
    }

    public boolean getCheckBoxValue()
    {
        return checkValue;
    }
}

As you can see, there is no `add` method in SWT. You just specify the parent in the constructor of each widget.

Moreover, here is a really good tutorial by Vogella, that explains in detail how to create a JFace dialog. Here is another example on how to use the `TitleAreaDialog`.

Problem

I am trying to create a base dialog class using SWT and JFace: ``` public class AplotBaseDialog extends TitleAreaDialog ``` I am confused about how to layout the dialog? Doing it in Swing I would have a `createDialog` method. Then in that method I would add Components that was JPanel methods. Then add the components to my centerPanel. Which was the base dialog. Each Panel method had their own layout. This is a very simple example (Pseudo Code) ``` public void createDialog() { Component selectionsPanel = createTableArea(); Component buttonPanel = OKCancelButtons(); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS)); centerPanel.add(selectionsPanel); centerPanel.add(buttonPanel); getContentPane().add(centerPanel); this.pack(); centerPanel.setVisible(true); } private JPanel OKCancelButtons() { submitButton = new JButton("Send"); etc... etc.. JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS)); p.add(submitButton); return p; } private JPanel createTableArea() { JPanel p = new JPanel(); similar to above but a Table; return p; } ``` You can see how I was creating the Panels in the methods than adding them to the base panel as components. How would you do that using `TitleAreaDialog`?

Original source