Sunday 8 January 2017

SWT: Color Dialog

By using ColorDialog, user can able to select a color from given colors.

Ex:
ColorDialog colorDialog1 = new ColorDialog(shell);
colorDialog1.setText("ColorDialog Demo");
colorDialog1.setRGB(new RGB(255, 100, 130));
RGB selectedColor = colorDialog1.open();

Following is the complete working application.
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  shell.open();
  ColorDialog colorDialog1 = new ColorDialog(shell);
  colorDialog1.setText("ColorDialog Demo");
  colorDialog1.setRGB(new RGB(255, 100, 130));
  RGB selectedColor = colorDialog1.open();

  System.out.println("Red : " + selectedColor.red);
  System.out.println("Green : " + selectedColor.green);
  System.out.println("Blue : " + selectedColor.blue);
 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(500, 500);
  addWidgetsToShell(display, shell);

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }

  /* Dispose the display */
  display.dispose();

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment