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

SWT: Message box

SWT provides MessageBox class to define MessageBox widget.

Ex:
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
messageBox.setMessage("Are you male?");
messageBox.setText("MessageBoxDemo");

You can create MessageBox with following styles.

ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING
OK, OK | CANCEL
YES | NO, YES | NO | CANCEL
RETRY | CANCEL
ABORT | RETRY | IGNORE

Note:
a. Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING and ICON_WORKING may be specified.

Following is the complete working application.
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  shell.open();
  MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
  messageBox.setMessage("Are you male?");
  messageBox.setText("MessageBoxDemo");
  int response = messageBox.open();

  switch (response) {
  case SWT.YES:
   System.out.println("Good Mornining Mr..");
   break;
  case SWT.NO:
   System.out.println("Good Morning Ms");
   break;
  case SWT.CANCEL:
   System.out.println("The user cancelled.");
   break;
  }

 }

 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();

 }
}


Run above application, you can able to see following screen.



Previous                                                 Next                                                 Home

Saturday, 7 January 2017

SWT: Design Splash screen

A splash screen is a graphical control element consisting of window containing an image, a logo and the current version of the software. A splash screen usually appears while a game or program is launching.

How to create splash screen in SWT?
Create a Shell instance with style 'SWT.NO_TRIM'.

Shell shell = new Shell(display, SWT.NO_TRIM);

Following is the complete working application.
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static String imagePath = "C:\\Users\\Downloads\\icons\\car.png";

 private static void addGraphicsToShell(Display display, Shell shell) {
  Image img = new Image(display, imagePath);
  ImageData imdata = img.getImageData();
  shell.setSize(imdata.width, imdata.height);

  Rectangle r = display.getBounds();
  int shellX = (r.width - imdata.width) / 2;
  int shellY = (r.height - imdata.height) / 2;
  shell.setLocation(shellX, shellY);

  shell.open();
  GC gc = new GC(shell);
  gc.drawImage(img, 0, 0);

  System.out.println("Sleeping for 5 seconds");
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  img.dispose();
  gc.dispose();
  shell.dispose();
  display.dispose();

 }

 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, SWT.NO_TRIM);
  addGraphicsToShell(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

Friday, 6 January 2017

Move files to recycle bin (or) trash

Some time when you are working with applications that performs CRUD operations on files, you need to move a file/folder to recycle bin rather than deleting permanently. By using ‘com.sun.jna.platform.FileUtils’ class, we can move a file/folder to recycle bin.

I used following maven dependency.

                 <dependency>
                          <groupId>net.java.dev.jna</groupId>
                          <artifactId>jna-platform</artifactId>
                          <version>4.0.0</version>
                 </dependency>

MoveToTrash.java
import java.io.File;
import java.io.IOException;

import com.sun.jna.platform.FileUtils;

public class MoveToTrash {

 /**
  * 
  * @param filePath
  * @return true on successful deletion of the file, filePath is null (or)
  *         empty. false in all other cases.
  */
 public static boolean moveFileToTrash(String filePath) {
  if (filePath == null || filePath.isEmpty()) {
   System.out.println("filePath shouldn't be null (or) empty");
   return true;
  }

  File file = new File(filePath);
  if (!file.exists()) {
   System.out.println("File is not exist, seems to be it is already deleted");
   return true;
  }

  FileUtils fileUtils = FileUtils.getInstance();
  if (fileUtils.hasTrash()) {
   try {
    fileUtils.moveToTrash(new File[] { new File(filePath) });
    return true;
   } catch (IOException e) {
    System.out.println("Error while moving the file to trash " + e.getMessage());
    return false;
   }
  } else {
   System.out.println("No Trash available");
  }

  return true;
 }

 public static void main(String[] args) {

  String filePath = "C:\\Users\\Proxy.docx";
  
  boolean isFileMovedToTrash = moveFileToTrash(filePath);
  
  if(isFileMovedToTrash){
   System.out.println("File is moved to trash " + filePath);
   return;
  }
  
  System.out.println("File is not moved to trash " + filePath);
 }
}

After running above application, it deletes the file "C:\\Users\\Proxy.docx" and move it to recycle bin on windows and to trash on Mac.

You may like


Thursday, 5 January 2017

SWT: Working with images

SWT provides Image class to draw images. Following step-by-step procedure explains sample application to draw images.

Step 1: Define an image instance and add it to the display.
String imagePath = "C:\\java.png";
Image img = new Image(display, imagePath);

Step 2: Use drawImage method of Graphic context instance.

GC gc = new GC(shell);
gc.drawImage(img, 200, 200);

Step 3: Dispose image and graphic context.
img.dispose();
gc.dispose();

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

public class Test {

 private static int shellWidth = 500;
 private static int shellHeight = 500;

 private static String imagePath = "java.png";

 private static void addGraphicsToShell(Display display, Shell shell) {
  Image img = new Image(display, imagePath);
  shell.open();
  GC gc = new GC(shell);
  gc.drawImage(img, 200, 200);
  img.dispose();
  gc.dispose();

 }

 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(shellWidth, shellHeight);
  shell.setText("SWT Tutorial");

  addGraphicsToShell(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();

 }
}

Run above application, you can able to see following window.




Previous                                                 Next                                                 Home

Wednesday, 4 January 2017

SWT: Working with fonts

SWT provides Font class to define font specific to your application. Font class provides following constructors to define Font instances.

         public Font(Device device, FontData fd)
         public Font(Device device, FontData[] fds)
         public Font(Device device, String name, int height, int style)

Ex:
Font font = new Font(display, "Arial", 10, SWT.BOLD | SWT.ITALIC);

Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.RGBA;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test
{

 private static int shellWidth = 1000;
 private static int shellHeight = 700;

 private static int canvasHeight = 300;
 private static int canvasWidth = 400;

 private static void drawGraphcis(Display display, GC gc)
 {
  Color blue = display.getSystemColor(SWT.COLOR_BLUE);
  Color red = display.getSystemColor(SWT.COLOR_RED);
  Color green = new Color(display, new RGB(0, 255, 0));
  Color color = new Color(display, new RGBA(24, 20, 123, 5));

  Font font = new Font(display, "Arial", 10, SWT.BOLD | SWT.ITALIC);
  gc.setFont(font);

  gc.setBackground(red);
  gc.drawRectangle(150, 130, 200, 75);
  gc.fillRectangle(30, 30, 200, 75);

  gc.setForeground(blue);
  gc.drawLine(0, 0, 400, 300);

  gc.drawString("Hello World Graphics Application", 75, 0);

  gc.drawOval(0, 0, 400, 300);

  green.dispose();
  color.dispose();
  font.dispose();

 }

 private static void addGraphicsToShell(Display display, Shell shell)
 {
  Canvas canvas = new Canvas(shell, SWT.BORDER);
  canvas.setSize(canvasWidth, canvasHeight);
  canvas.setLocation(20, 20);

  /* Open shell window */
  shell.open();

  GC gc = new GC(canvas);

  drawGraphcis(display, gc);

  canvas.addPaintListener(new PaintListener()
  {
   @Override
   public void paintControl(PaintEvent e)
   {
    GC gc = e.gc;
    drawGraphcis(display, gc);
   }

  });
  gc.dispose();

 }

 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(shellWidth, shellHeight);
  shell.setText("SWT Tutorial");

  addGraphicsToShell(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

Tuesday, 3 January 2017

SWT: Working with colors

SWT provides Color class to define colors. There are three ways to get Color object.
a.   Using getSystemColor method
b.   Using RGB
c.    Using RGBA

Using getSystemColor method
Display class provides 'getSystemColor' method to get the color instance.
public Color getSystemColor (int id)

Ex:
Color blue = display.getSystemColor(SWT.COLOR_BLUE);

Using RGB
Color class provides following constructor to define color instance using RGB notation.

public Color (Device device, RGB rgb)

Ex:
Color green = new Color(display, new RGB(0, 255, 0));

Using RGBA
Color class provide following constructor to define color instance using RGBA notation.

public Color(Device device, RGBA rgba)

Ex:
Color color = new Color(display, new RGBA(24, 20, 123, 5));

If you create Color instances using any approach other than getSystemColor method, you need to dispose them manually.

Ex:
green.dispose();
color.dispose();

Following is the complete working application.

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.RGBA;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test
{

 private static int shellWidth = 1000;
 private static int shellHeight = 700;

 private static int canvasHeight = 300;
 private static int canvasWidth = 400;

 private static void drawGraphcis(Display display, GC gc)
 {
  Color blue = display.getSystemColor(SWT.COLOR_BLUE);
  Color red = display.getSystemColor(SWT.COLOR_RED);
  Color green = new Color(display, new RGB(0, 255, 0));
  Color color = new Color(display, new RGBA(24, 20, 123, 5));

  gc.setBackground(red);
  gc.drawRectangle(150, 130, 200, 75);
  gc.fillRectangle(30, 30, 200, 75);

  gc.setForeground(blue);
  gc.drawLine(0, 0, 400, 300);

  gc.drawString("Hello World Graphics Application", 75, 0);

  gc.drawOval(0, 0, 400, 300);

  green.dispose();
  color.dispose();

 }

 private static void addGraphicsToShell(Display display, Shell shell)
 {
  Canvas canvas = new Canvas(shell, SWT.BORDER);
  canvas.setSize(canvasWidth, canvasHeight);
  canvas.setLocation(20, 20);

  /* Open shell window */
  shell.open();

  GC gc = new GC(canvas);

  drawGraphcis(display, gc);

  canvas.addPaintListener(new PaintListener()
  {
   @Override
   public void paintControl(PaintEvent e)
   {
    GC gc = e.gc;
    drawGraphcis(display, gc);
   }

  });
  gc.dispose();

 }

 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(shellWidth, shellHeight);
  shell.setText("SWT Tutorial");

  addGraphicsToShell(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