All pastes #792473 Raw Edit

Attach Form

public java v1 · immutable
·published 2007-11-21 13:03 UTC
rendered paste body
import org.eclipse.swt.*;import org.eclipse.swt.events.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.widgets.*;/*** This class demonstrates the ColorDialog class*/public class ChooseColor {private Color color;/*** Runs the application*/public void run() {Display display = new Display();Shell shell = new Shell(display);shell.setText("Color Chooser");createContents(shell);shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}// Dispose the color we created for the Labelif (color != null) {color.dispose();}display.dispose();}/*** Creates the window contents** @param shell the parent shell*/private void createContents(final Shell shell) {shell.setLayout(new GridLayout(2, false));// Start with Celtics greencolor = new Color(shell.getDisplay(), new RGB(0, 255, 0));// Use a label full of spaces to show the colorfinal Label colorLabel = new Label(shell, SWT.NONE);colorLabel.setText(" ");colorLabel.setBackground(color);Button button = new Button(shell, SWT.PUSH);button.setText("Color...");button.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {// Create the color-change dialogColorDialog dlg = new ColorDialog(shell);// Set the selected color in the dialog from// user's selected colordlg.setRGB(colorLabel.getBackground().getRGB());// Change the title bar textdlg.setText("Choose a Color");// Open the dialog and retrieve the selected colorRGB rgb = dlg.open();if (rgb != null) {// Dispose the old color, create the// new one, and set into the labelcolor.dispose();color = new Color(shell.getDisplay(), rgb);colorLabel.setBackground(color);}}});}/*** The application entry point** @param args the command line arguments*/public static void main(String[] args) {new ChooseColor().run();}}