ControlEditorWithDialog.java Source code

Java tutorial

Introduction

Here is the source code for ControlEditorWithDialog.java

Source

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ControlEditorWithDialog {
    static Display display = new Display();

    static Color color = new Color(display, 255, 0, 0);

    public static void main(String[] args) {

        final Shell shell = new Shell(display);
        shell.setText("Control Editor Two");

        final Composite composite = new Composite(shell, SWT.NONE);
        composite.setBackground(color);
        composite.setBounds(0, 0, 300, 100);

        ControlEditor editor = new ControlEditor(composite);

        // Create the control associated with the editor
        Button button = new Button(composite, SWT.PUSH);
        button.setText("Change Color...");
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                ColorDialog dialog = new ColorDialog(shell);
                if (color != null)
                    dialog.setRGB(color.getRGB());
                RGB rgb = dialog.open();
                if (rgb != null) {
                    if (color != null)
                        color.dispose();
                    color = new Color(shell.getDisplay(), rgb);
                    composite.setBackground(color);
                }
            }
        });
        // Place the editor along the bottom of the parent composite
        editor.grabHorizontal = true;
        editor.verticalAlignment = SWT.BOTTOM;
        Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        editor.minimumHeight = size.y;
        editor.setEditor(button);

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        if (color != null)
            color.dispose();
        display.dispose();
    }
}