chosenBackgroundColor = defaultBackgroundColor.getRGB(); chosenForegroundColor = defaultForegroundColor.getRGB(); GC gc = new GC(fontButton.getShell()); final int fontHeight = gc.getFontMetrics().getHeight(); gc.dispose (); backgroundImage = new Image(colorGroup.getDisplay(),fontHeight*2,fontHeight); gc = new GC(backgroundImage); gc.setBackground(defaultBackgroundColor); gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1); gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1); gc.dispose(); foregroundImage = new Image(colorGroup.getDisplay(),fontHeight*2,fontHeight); gc = new GC(foregroundImage); gc.setBackground(defaultForegroundColor); gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1); gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1); gc.dispose();
int imageSize = 12; Display display = shell.getDisplay (); final Image backgroundImage = new Image (display, imageSize, imageSize); final Image foregroundImage = new Image (display, imageSize, imageSize);
	void createColorGroup () {		/* Create the group */		colorGroup = new Group(controlGroup, SWT.NULL);		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 ("Background_Color"));		backgroundButton = new Button (colorGroup, SWT.PUSH);		new Label (colorGroup, SWT.NONE).setText (ControlExample.getResourceString ("Foreground_Color"));		foregroundButton = new Button (colorGroup, SWT.PUSH);		fontButton = new Button (colorGroup, SWT.PUSH);		fontButton.setText(ControlExample.getResourceString("Font"));				/* Create images to display current colors */		chosenBackgroundColor = defaultBackgroundColor.getRGB();		chosenForegroundColor = defaultForegroundColor.getRGB();		GC gc = new GC(fontButton.getShell());		final int fontHeight = gc.getFontMetrics().getHeight();		gc.dispose ();		backgroundImage = new Image(colorGroup.getDisplay(),fontHeight*2,fontHeight);		gc = new GC(backgroundImage);		gc.setBackground(defaultBackgroundColor);		gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1);		gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1);		gc.dispose();		foregroundImage = new Image(colorGroup.getDisplay(),fontHeight*2,fontHeight);		gc = new GC(foregroundImage);		gc.setBackground(defaultForegroundColor);		gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1);		gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1);		gc.dispose();				/* Add listeners to set the colors and font */		backgroundButton.setImage(backgroundImage);		backgroundButton.addSelectionListener(new SelectionAdapter() {			public void widgetSelected(SelectionEvent event) {				ColorDialog colorDialog= new ColorDialog(backgroundButton.getShell());				colorDialog.setRGB(chosenBackgroundColor);				RGB newColor = colorDialog.open();				if (newColor != null) {					Color oldColor = null;					if (backgroundColor != null) oldColor = backgroundColor;					backgroundColor = new Color (backgroundButton.getDisplay(), newColor);					GC gc = new GC(backgroundImage);					gc.setBackground(backgroundColor);					gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1);					gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1);					gc.dispose();					backgroundButton.setImage(backgroundImage);					chosenBackgroundColor = newColor;					setColors ();					if (oldColor != null) oldColor.dispose ();				}			}		});		foregroundButton.setImage(foregroundImage);		foregroundButton.addSelectionListener(new SelectionAdapter() {			public void widgetSelected(SelectionEvent event) {				ColorDialog colorDialog= new ColorDialog(foregroundButton.getShell());				colorDialog.setRGB(chosenForegroundColor);				RGB newColor = colorDialog.open();				if (newColor != null) {					Color oldColor = null;					if (foregroundColor != null) oldColor = foregroundColor;					foregroundColor = new Color (foregroundButton.getDisplay(), newColor);					GC gc= new GC(foregroundImage);					gc.setBackground(foregroundColor);					gc.fillRectangle(0,0,fontHeight*2-1,fontHeight-1);					gc.drawRectangle(0,0,fontHeight*2-1,fontHeight-1);					gc.dispose();					foregroundButton.setImage(foregroundImage);					chosenForegroundColor = newColor;					setColors ();					if (oldColor != null) oldColor.dispose ();				}			}		});		fontButton.addSelectionListener(new SelectionAdapter () {			public void widgetSelected (SelectionEvent e) {				FontDialog fontDialog = new FontDialog (fontButton.getShell());				if (chosenFont == null) {					fontDialog.setFontData ((fontButton.getFont().getFontData ())[0]);				} else {					fontDialog.setFontData(chosenFont);				}				FontData fontData = fontDialog.open ();				if (fontData != null) {					if (font != null) font.dispose ();					font = new Font (fontButton.getDisplay(), fontData);					chosenFont = fontData;				}			}		});		backgroundButton.addDisposeListener(new DisposeListener() {			public void widgetDisposed(DisposeEvent event) {				if (backgroundImage != null) backgroundImage.dispose();				if (foregroundImage != null) foregroundImage.dispose();				if (backgroundColor != null) backgroundColor.dispose();				if (foregroundColor != null) foregroundColor.dispose();				if (font != null) font.dispose();				backgroundImage = null;				foregroundImage = null;				backgroundColor = null;				foregroundColor = null;				font = null;							}		});	}