Example usage for org.eclipse.swt.widgets Button setText

List of usage examples for org.eclipse.swt.widgets Button setText

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button setText.

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java

int showBMPDialog() {
    final int[] bmpType = new int[1];
    bmpType[0] = SWT.IMAGE_BMP;//  w ww. j a v a 2 s  .  c o  m
    SelectionListener radioSelected = widgetSelectedAdapter(event -> {
        Button radio = (Button) event.widget;
        if (radio.getSelection())
            bmpType[0] = ((Integer) radio.getData()).intValue();
    });
    // need to externalize strings
    final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);

    dialog.setText(bundle.getString("Save_as_type"));
    dialog.setLayout(new GridLayout());

    Label label = new Label(dialog, SWT.NONE);
    label.setText(bundle.getString("Save_as_type_label"));

    Button radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_no_compress"));
    radio.setSelection(true);
    radio.setData(Integer.valueOf(SWT.IMAGE_BMP));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_rle_compress"));
    radio.setData(Integer.valueOf(SWT.IMAGE_BMP_RLE));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_os2"));
    radio.setData(Integer.valueOf(SWT.IMAGE_OS2_BMP));
    radio.addSelectionListener(radioSelected);

    label = new Label(dialog, SWT.SEPARATOR | SWT.HORIZONTAL);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText(bundle.getString("OK"));
    GridData data = new GridData();
    data.horizontalAlignment = SWT.CENTER;
    data.widthHint = 75;
    ok.setLayoutData(data);
    ok.addSelectionListener(widgetSelectedAdapter(e -> dialog.close()));

    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    return bmpType[0];
}

From source file:PaintExample.java

/**
 * Handles a mouseDown event./*from  w  ww  . java 2 s. co m*/
 * 
 * @param event the mouse event detail information
 */
public void mouseDown(MouseEvent event) {
    if (event.button == 1) {
        // draw with left mouse button
        getPaintSurface().commitRubberbandSelection();
    } else {
        // set text with right mouse button
        getPaintSurface().clearRubberbandSelection();
        Shell shell = getPaintSurface().getShell();
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        dialog.setText(PaintExample.getResourceString("tool.Text.dialog.title"));
        dialog.setLayout(new GridLayout());
        Label label = new Label(dialog, SWT.NONE);
        label.setText(PaintExample.getResourceString("tool.Text.dialog.message"));
        label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        final Text field = new Text(dialog, SWT.SINGLE | SWT.BORDER);
        field.setText(drawText);
        field.selectAll();
        field.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        Composite buttons = new Composite(dialog, SWT.NONE);
        GridLayout layout = new GridLayout(2, true);
        layout.marginWidth = 0;
        buttons.setLayout(layout);
        buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
        Button ok = new Button(buttons, SWT.PUSH);
        ok.setText(PaintExample.getResourceString("OK"));
        ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        ok.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                drawText = field.getText();
                dialog.dispose();
            }
        });
        Button cancel = new Button(buttons, SWT.PUSH);
        cancel.setText(PaintExample.getResourceString("Cancel"));
        cancel.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                dialog.dispose();
            }
        });
        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
        Display display = dialog.getDisplay();
        while (!shell.isDisposed() && !dialog.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

From source file:PrintKTableExample.java

/**
 * Fugt einen Button zur Leiste hinzu. Gibt eine Referenz auf den angelegten
 * Button zuruck./*  ww w  . j  a  v  a 2  s  .  com*/
 */
public Button addButton(String name, String toolTip, SelectionListener selListener) {
    Button b = new Button(this, SWT.PUSH);
    b.setText(name);
    b.setToolTipText(toolTip);
    b.setLayoutData(new RowData(myButtonWidth, 25));
    if (selListener != null)
        b.addSelectionListener(selListener);
    myButtons.add(b);
    return b;
}

From source file:PrintKTableExample.java

protected Button addButton(boolean rightAdjusted, String text, String tip) {
    Button erg = new Button(guiButtonArea, SWT.PUSH);
    erg.setText(text);
    erg.setToolTipText(tip);/*  w  w  w  .j  a v  a 2s .c  o  m*/

    Point butPrefferedSize = erg.computeSize(SWT.DEFAULT, SWT.DEFAULT);

    FormData fd = new FormData();
    fd.bottom = new FormAttachment(100, -3);

    if (butPrefferedSize.x > 70)
        fd.width = butPrefferedSize.x + 4;
    else
        fd.width = 70;
    fd.height = 24;
    erg.setLayoutData(fd);

    if (rightAdjusted) {
        if (guiLastRightBut == null)
            fd.right = new FormAttachment(100, -3);
        else
            fd.right = new FormAttachment(guiLastRightBut, -3);
        guiLastRightBut = erg;
    } else {
        if (guiLastLeftBut == null)
            fd.left = new FormAttachment(0, 3);
        else
            fd.left = new FormAttachment(guiLastLeftBut, 3);
        guiLastLeftBut = erg;
    }

    erg.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent arg0) {
            onButton((Button) arg0.widget, ((Button) arg0.widget).getText());
        }
    });
    return erg;
}

From source file:CustomControlExample.java

/**
 * Append the Set/Get API controls to the "Other" group.
 *//*w  w  w.  j  a v  a 2s  .  co  m*/
void createSetGetGroup() {
    /*
     * Create the button to access set/get API functionality.
     */
    final String[] methodNames = getMethodNames();
    if (methodNames != null) {
        Button setGetButton = new Button(otherGroup, SWT.PUSH);
        setGetButton.setText(ControlExample.getResourceString("Set_Get"));
        setGetButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        setGetButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Button button = (Button) e.widget;
                Point pt = button.getLocation();
                pt = e.display.map(button, null, pt);
                createSetGetDialog(pt.x, pt.y, methodNames);
            }
        });
    }
}

From source file:CustomControlExample.java

/**
 * Creates and opens the "Listener selection" dialog.
 *//*from  ww w  .  j a  v  a2s.  c  o  m*/
void createListenerSelectionDialog() {
    final Shell dialog = new Shell(tabFolderPage.getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    dialog.setText(ControlExample.getResourceString("Select_Listeners"));
    dialog.setLayout(new GridLayout(2, false));
    final Table table = new Table(dialog, SWT.BORDER | SWT.V_SCROLL | SWT.CHECK);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.verticalSpan = 2;
    table.setLayoutData(data);
    for (int i = 0; i < EVENT_NAMES.length; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(EVENT_NAMES[i]);
        item.setChecked(eventsFilter[i]);
    }
    final String[] customNames = getCustomEventNames();
    for (int i = 0; i < customNames.length; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(customNames[i]);
        item.setChecked(eventsFilter[EVENT_NAMES.length + i]);
    }
    Button selectAll = new Button(dialog, SWT.PUSH);
    selectAll.setText(ControlExample.getResourceString("Select_All"));
    selectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    selectAll.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] items = table.getItems();
            for (int i = 0; i < EVENT_NAMES.length; i++) {
                items[i].setChecked(true);
            }
            for (int i = 0; i < customNames.length; i++) {
                items[EVENT_NAMES.length + i].setChecked(true);
            }
        }
    });
    Button deselectAll = new Button(dialog, SWT.PUSH);
    deselectAll.setText(ControlExample.getResourceString("Deselect_All"));
    deselectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING));
    deselectAll.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] items = table.getItems();
            for (int i = 0; i < EVENT_NAMES.length; i++) {
                items[i].setChecked(false);
            }
            for (int i = 0; i < customNames.length; i++) {
                items[EVENT_NAMES.length + i].setChecked(false);
            }
        }
    });
    new Label(dialog, SWT.NONE); /* Filler */
    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText(ControlExample.getResourceString("OK"));
    dialog.setDefaultButton(ok);
    ok.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    ok.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] items = table.getItems();
            for (int i = 0; i < EVENT_NAMES.length; i++) {
                eventsFilter[i] = items[i].getChecked();
            }
            for (int i = 0; i < customNames.length; i++) {
                eventsFilter[EVENT_NAMES.length + i] = items[EVENT_NAMES.length + i].getChecked();
            }
            dialog.dispose();
        }
    });
    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
        if (!dialog.getDisplay().readAndDispatch())
            dialog.getDisplay().sleep();
    }
}

From source file:CustomControlExample.java

/**
 * Creates the "Listeners" group. The "Listeners" group goes below the
 * "Example" and "Control" groups./*  w  w w. j av  a 2  s .  co m*/
 */
void createListenersGroup() {
    listenersGroup = new Group(tabFolderPage, SWT.NONE);
    listenersGroup.setLayout(new GridLayout(3, false));
    listenersGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 2, 1));
    listenersGroup.setText(ControlExample.getResourceString("Listeners"));

    /*
     * Create the button to access the 'Listeners' dialog.
     */
    Button listenersButton = new Button(listenersGroup, SWT.PUSH);
    listenersButton.setText(ControlExample.getResourceString("Select_Listeners"));
    listenersButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            createListenerSelectionDialog();
            recreateExampleWidgets();
        }
    });

    /*
     * Create the checkbox to add/remove listeners to/from the example
     * widgets.
     */
    final Button listenCheckbox = new Button(listenersGroup, SWT.CHECK);
    listenCheckbox.setText(ControlExample.getResourceString("Listen"));
    listenCheckbox.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            logging = listenCheckbox.getSelection();
            recreateExampleWidgets();
        }
    });

    /*
     * Create the button to clear the text.
     */
    Button clearButton = new Button(listenersGroup, SWT.PUSH);
    clearButton.setText(ControlExample.getResourceString("Clear"));
    clearButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
    clearButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            eventConsole.setText("");
        }
    });

    /* Initialize the eventsFilter to log all events. */
    int customEventCount = getCustomEventNames().length;
    eventsFilter = new boolean[EVENT_NAMES.length + customEventCount];
    for (int i = 0; i < EVENT_NAMES.length + customEventCount; i++) {
        eventsFilter[i] = true;
    }

    /* Create the event console Text. */
    eventConsole = new Text(listenersGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 3;
    data.heightHint = 80;
    eventConsole.setLayoutData(data);
    createEventConsolePopup();
    eventConsole.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if ((e.keyCode == 'A' || e.keyCode == 'a') && (e.stateMask & SWT.MOD1) != 0) {
                eventConsole.selectAll();
                e.doit = false;
            }
        }
    });
}

From source file:CustomControlExample.java

/**
 * Handle the Create button selection event.
 * //from   w  w  w .j  a v a 2  s  .  co m
 * @param event
 *            org.eclipse.swt.events.SelectionEvent
 */
public void createButtonSelected(SelectionEvent event) {

    /*
     * Remember the example shells so they can be disposed by the user.
     */
    if (shellCount >= shells.length) {
        Shell[] newShells = new Shell[shells.length + 4];
        System.arraycopy(shells, 0, newShells, 0, shells.length);
        shells = newShells;
    }

    /* Compute the shell style */
    int style = SWT.NONE;
    if (noTrimButton.getSelection())
        style |= SWT.NO_TRIM;
    if (closeButton.getSelection())
        style |= SWT.CLOSE;
    if (titleButton.getSelection())
        style |= SWT.TITLE;
    if (minButton.getSelection())
        style |= SWT.MIN;
    if (maxButton.getSelection())
        style |= SWT.MAX;
    if (borderButton.getSelection())
        style |= SWT.BORDER;
    if (resizeButton.getSelection())
        style |= SWT.RESIZE;
    if (onTopButton.getSelection())
        style |= SWT.ON_TOP;
    if (toolButton.getSelection())
        style |= SWT.TOOL;
    if (modelessButton.getSelection())
        style |= SWT.MODELESS;
    if (primaryModalButton.getSelection())
        style |= SWT.PRIMARY_MODAL;
    if (applicationModalButton.getSelection())
        style |= SWT.APPLICATION_MODAL;
    if (systemModalButton.getSelection())
        style |= SWT.SYSTEM_MODAL;

    /* Create the shell with or without a parent */
    if (noParentButton.getSelection()) {
        shells[shellCount] = new Shell(style);
    } else {
        Shell shell = tabFolderPage.getShell();
        shells[shellCount] = new Shell(shell, style);
    }
    final Shell currentShell = shells[shellCount];
    Button button = new Button(currentShell, SWT.PUSH);
    button.setBounds(20, 20, 120, 30);
    Button closeButton = new Button(currentShell, SWT.PUSH);
    closeButton.setBounds(160, 20, 120, 30);
    closeButton.setText(ControlExample.getResourceString("Close"));
    closeButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            currentShell.dispose();
        }
    });

    /* Set the size, title, and image, and open the shell */
    currentShell.setSize(300, 100);
    currentShell.setText(ControlExample.getResourceString("Title") + shellCount);
    if (imageButton.getSelection())
        currentShell.setImage(instance.images[ControlExample.ciTarget]);
    hookListeners(currentShell);
    currentShell.open();
    shellCount++;
}

From source file:CustomControlExample.java

/**
 * Creates the "Colors" group. This is typically a child of the "Control"
 * group. Subclasses override this method to customize and set system
 * colors./*  w  ww.ja va2 s.co  m*/
 */
void createColorGroup() {
    /* Create the group */
    colorGroup = new Group(controlGroup, SWT.NONE);
    colorGroup.setLayout(new GridLayout(2, false));
    colorGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    colorGroup.setText(ControlExample.getResourceString("Colors"));
    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    foregroundButton = new Button(colorGroup, SWT.PUSH);
    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    backgroundButton = new Button(colorGroup, SWT.PUSH);
    fontButton = new Button(colorGroup, SWT.PUSH);
    fontButton.setText(ControlExample.getResourceString("Font"));
    fontButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    Button defaultsButton = new Button(colorGroup, SWT.PUSH);
    defaultsButton.setText(ControlExample.getResourceString("Defaults"));

    Shell shell = controlGroup.getShell();
    final ColorDialog foregroundDialog = new ColorDialog(shell);
    final ColorDialog backgroundDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    /* Create images to display current colors */
    int imageSize = 12;
    Display display = shell.getDisplay();
    foregroundImage = new Image(display, imageSize, imageSize);
    backgroundImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    foregroundButton.setImage(foregroundImage); // sets the size of the
    // button
    foregroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                foregroundDialog.setRGB(oldColor.getRGB()); // seed dialog
            // with current
            // color
            RGB rgb = foregroundDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundColor; // save old foreground color to
            // dispose when done
            foregroundColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundButton.setImage(backgroundImage); // sets the size of the
    // button
    backgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                backgroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = backgroundDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundColor; // save old background color to
            // dispose when done
            backgroundColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    fontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = font;
            if (oldFont == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldFont = controls[0].getFont();
            }
            if (oldFont != null)
                fontDialog.setFontList(oldFont.getFontData()); // seed
            // dialog
            // with
            // current
            // font
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = font; // dispose old font when done
            font = new Font(event.display, fontData);
            setExampleWidgetFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });
    defaultsButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            resetColorsAndFonts();
        }
    });
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (foregroundImage != null)
                foregroundImage.dispose();
            if (backgroundImage != null)
                backgroundImage.dispose();
            if (foregroundColor != null)
                foregroundColor.dispose();
            if (backgroundColor != null)
                backgroundColor.dispose();
            if (font != null)
                font.dispose();
            foregroundColor = null;
            backgroundColor = null;
            font = null;
        }
    });
}

From source file:CustomControlExample.java

/**
 * Creates the "Fonts" group.//from ww w  .  j  a va  2 s. co m
 */
void createColorGroup() {
    /* Create the group */
    colorGroup = new Group(controlGroup, SWT.NONE);
    colorGroup.setLayout(new GridLayout(2, false));
    colorGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    colorGroup.setText(ControlExample.getResourceString("Colors"));

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    foregroundButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    backgroundButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Selection_Foreground_Color"));
    foregroundSelectionButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Selection_Background_Color"));
    backgroundSelectionButton = new Button(colorGroup, SWT.PUSH);

    fontButton = new Button(colorGroup, SWT.PUSH);
    fontButton.setText(ControlExample.getResourceString("Font"));
    fontButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

    itemFontButton = new Button(colorGroup, SWT.PUSH);
    itemFontButton.setText(ControlExample.getResourceString("Item_Font"));
    itemFontButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

    Button defaultsButton = new Button(colorGroup, SWT.PUSH);
    defaultsButton.setText(ControlExample.getResourceString("Defaults"));

    Shell shell = controlGroup.getShell();
    final ColorDialog colorDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    /* Create images to display current colors */
    int imageSize = 12;
    Display display = shell.getDisplay();
    foregroundImage = new Image(display, imageSize, imageSize);
    backgroundImage = new Image(display, imageSize, imageSize);
    foregroundSelectionImage = new Image(display, imageSize, imageSize);
    backgroundSelectionImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    foregroundButton.setImage(foregroundImage); // sets the size of the
    // button
    foregroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB()); // seed dialog with
            // current color
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundColor; // save old foreground color to
            // dispose when done
            foregroundColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundButton.setImage(backgroundImage); // sets the size of the
    // button
    backgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB());
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundColor; // save old background color to
            // dispose when done
            backgroundColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    foregroundSelectionButton.setImage(foregroundSelectionImage); // sets
    // the
    // size
    // of
    // the
    // button
    foregroundSelectionButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundSelectionColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB()); // seed dialog with
            // current color
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundSelectionColor; // save old foreground
            // color to dispose when
            // done
            foregroundSelectionColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundSelectionButton.setImage(backgroundSelectionImage); // sets
    // the
    // size
    // of
    // the
    // button
    backgroundSelectionButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundSelectionColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB());
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundSelectionColor; // save old background
            // color to dispose when
            // done
            backgroundSelectionColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    fontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = font;
            if (oldFont == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldFont = controls[0].getFont();
            }
            if (oldFont != null)
                fontDialog.setFontList(oldFont.getFontData()); // seed
            // dialog
            // with
            // current
            // font
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = font; // dispose old font when done
            font = new Font(event.display, fontData);
            setExampleWidgetFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });

    /* Add listeners to set the colors and font */
    itemFontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = itemFont;
            if (oldFont == null)
                oldFont = tabFolder1.getItem(0).getFont();
            fontDialog.setFontList(oldFont.getFontData());
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = itemFont;
            itemFont = new Font(event.display, fontData);
            setItemFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });

    defaultsButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            resetColorsAndFonts();
        }
    });

    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (foregroundImage != null)
                foregroundImage.dispose();
            if (backgroundImage != null)
                backgroundImage.dispose();
            if (foregroundColor != null)
                foregroundColor.dispose();
            if (backgroundColor != null)
                backgroundColor.dispose();
            if (font != null)
                font.dispose();
            foregroundColor = null;
            backgroundColor = null;
            font = null;
            if (foregroundSelectionImage != null)
                foregroundSelectionImage.dispose();
            if (backgroundSelectionImage != null)
                backgroundSelectionImage.dispose();
            if (foregroundSelectionColor != null)
                foregroundSelectionColor.dispose();
            if (backgroundSelectionColor != null)
                backgroundSelectionColor.dispose();
            foregroundSelectionColor = null;
            backgroundSelectionColor = null;
            if (itemFont != null)
                itemFont.dispose();
            itemFont = null;
        }
    });
}