Friday, 13 January 2017

SWT: RowLayout

RowLayout arrange the widgets in a row. You can appy horizontal (or) vertical styles to layout. RowLayout provides additional fields to control the user interface.

Ex:
RowLayout rowLayout = new RowLayout();
rowLayout.pack = false;
rowLayout.wrap = true;
rowLayout.justify = true;
rowLayout.type = SWT.VERTICAL;
rowLayout.marginLeft = 5;
rowLayout.marginTop = 5;
rowLayout.marginRight = 5;
rowLayout.marginBottom = 5;
rowLayout.spacing = 0;

Pack field makes sure that all the fields are in same size,  and they will be placed as far to the left (or the top) as possible.

If you set wrap feature to true, long rows (Which are beyond window size) will wrap around to form two rows.

Following is the complete working application.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {

  RowLayout rowLayout = new RowLayout();
  // rowLayout.pack = true;
  // rowLayout.wrap = true;
  // rowLayout.justify = true;
  // rowLayout.type = SWT.VERTICAL;
  // rowLayout.marginLeft = 5;
  // rowLayout.marginTop = 5;
  // rowLayout.marginRight = 5;
  // rowLayout.marginBottom = 5;
  // rowLayout.spacing = 0;

  shell.setLayout(rowLayout);

  Button b1 = new Button(shell, SWT.PUSH);
  b1.setText("File");
  Button b2 = new Button(shell, SWT.PUSH);
  b2.setText("Edit");
  Button b3 = new Button(shell, SWT.PUSH);
  b3.setText("Source");
  Button b4 = new Button(shell, SWT.PUSH);
  b4.setText("Refactor");
  Button b5 = new Button(shell, SWT.PUSH);
  b5.setText("Navigate");
  Button b6 = new Button(shell, SWT.PUSH);
  b6.setText("Search");
  Button b7 = new Button(shell, SWT.PUSH);
  b7.setText("Search");
  Button b8 = new Button(shell, SWT.PUSH);
  b8.setText("Search");
  Button b9 = new Button(shell, SWT.PUSH);
  b9.setText("Search");
  Button b10 = new Button(shell, SWT.PUSH);
  b10.setText("Search");

  shell.open();
 }

 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(700, 300);
  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 screens.
Try to minimize the window size, you can able to see screens like below.





Previous                                                 Next                                                 Home

Thursday, 12 January 2017

SWT: FillLayout

FillLayout arranges the swt widgets horizontally in a row (or) vertically in a column. It forces all the widgets to be in same size. Horizontal is the default layout.

Following statements define vertical layout.

FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;

You can set the layout of a shell using setLayout method.
shell.setLayout(fillLayout);

Following is the complete working application.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {

  FillLayout fillLayout = new FillLayout();
  // fillLayout.type = SWT.VERTICAL;

  shell.setLayout(fillLayout);
  Label label0 = new Label(shell, SWT.NONE);
  label0.setText("File");
  Label label1 = new Label(shell, SWT.NONE);
  label1.setText("Edit");
  Label label2 = new Label(shell, SWT.NONE);
  label2.setText("Source");
  Label label3 = new Label(shell, SWT.NONE);
  label3.setText("Refactor");
  Label label4 = new Label(shell, SWT.NONE);
  label4.setText("Navigate");
  Label label5 = new Label(shell, SWT.NONE);
  label5.setText("Search");
  Label label6 = new Label(shell, SWT.NONE);
  label6.setText("Project");
  Label label7 = new Label(shell, SWT.NONE);
  label7.setText("Run");
  Label label8 = new Label(shell, SWT.NONE);
  label8.setText("Window");
  Label label9 = new Label(shell, SWT.NONE);
  label9.setText("Help");
  shell.open();
 }

 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(800, 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

SWT: Layouts

Primarily, there are four layouts, You can build SWT user interface using these layouts.

a.   FillLayout
b.   RowLayout
c.    GridLayout
d.   FormLayout

By using layout we can control the position and size of children in composite.


Previous                                                 Next                                                 Home

Wednesday, 11 January 2017

SWT: printDialog: Select a printer

By using PrintDialog widget, user can select a printer from various available printers in the system.

Ex:
PrintDialog printDialog = new PrintDialog(shell, SWT.NONE);
printDialog.setText("PrintDialogDemo");
printDialog.setScope(PrinterData.PAGE_RANGE);
printDialog.setStartPage(2);
printDialog.setEndPage(5);
printDialog.setPrintToFile(true);
PrinterData printerData = printDialog.open();

Following is the complete working application.
import java.util.Objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  PrintDialog printDialog = new PrintDialog(shell, SWT.NONE);
  printDialog.setText("PrintDialogDemo");
  printDialog.setScope(PrinterData.PAGE_RANGE);
  printDialog.setStartPage(2);
  printDialog.setEndPage(5);
  printDialog.setPrintToFile(true);
  PrinterData printerData = printDialog.open();

  if (!Objects.isNull(printerData)) {
   switch (printerData.scope) {
   case PrinterData.ALL_PAGES:
    System.out.println("Printing all pages.");
    break;
   case PrinterData.SELECTION:
    System.out.println("Printing selected page.");
    break;
   case PrinterData.PAGE_RANGE:
    System.out.print("Printing page range. ");
    System.out.print("From:" + printerData.startPage);
    System.out.println(" to:" + printerData.endPage);
    break;
   }
   if (printerData.printToFile)
    System.out.println("Printing to file.");
   else
    System.out.println("Not printing to file.");
   if (printerData.collate)
    System.out.println("Collating.");
   else
    System.out.println("Not collating.");
   System.out.println("Number of copies:" + printerData.copyCount);
   System.out.println("Printer Name:" + printerData.name);
  }
  shell.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(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 window.


Previous                                                 Next                                                 Home

Tuesday, 10 January 2017

SWT: FontDialog

By using FontDialog, user can select a font from fonts available in the system.

Ex:
FontData defaultFontData = new FontData("Courier", 12, SWT.ITALIC);
FontData[] fontList = { defaultFontData };
FontDialog fontDialog = new FontDialog(shell, SWT.NONE);
fontDialog.setText("FontDialog Demo");
fontDialog.setRGB(new RGB(0, 0, 255));
fontDialog.setFontList(fontList);
FontData fontData = fontDialog.open();


Following is the complete working application.
import java.util.Objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  FontData defaultFontData = new FontData("Courier", 12, SWT.ITALIC);
  FontData[] fontList = { defaultFontData };
  FontDialog fontDialog = new FontDialog(shell, SWT.NONE);
  fontDialog.setText("FontDialog Demo");
  fontDialog.setRGB(new RGB(0, 0, 255));
  fontDialog.setFontList(fontList);
  FontData fontData = fontDialog.open();

  if (!Objects.isNull(fontData)) {
   System.out.println("Font Name Selected:" + fontData.getName());
   System.out.println("Font Height Selected:" + fontData.getHeight());
   System.out.println("Font Style Selected:" + fontData.getStyle());
   System.out.println("Font Colour Selected:" + fontDialog.getRGB());
  }
  shell.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(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: FileDialog

By using DirectoryDialog widget, user can able to navigate through the file system and select a file.

Ex:
String[] filterExtensions = { "*.txt", "*.doc", "*.*"};
FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
fileDialog.setText("FileDialog Demo");
fileDialog.setFilterPath("C:/");
fileDialog.setFilterExtensions(filterExtensions);
String selectedFile = fileDialog.open();

Following is the complete working application.

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

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  String[] filterExtensions = { "*.txt", "*.doc", "*.*"};
  FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
  fileDialog.setText("FileDialog Demo");
  fileDialog.setFilterPath("C:/");
  fileDialog.setFilterExtensions(filterExtensions);
  String selectedFile = fileDialog.open();

  System.out.println("User selected : " + selectedFile);
  shell.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(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

Monday, 9 January 2017

SWT: DirectoryDialog

By using DirectoryDialog widget, user can able to navigate through the directory system.

Ex:
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
directoryDialog.setText("DirectoryDialog Demo");
directoryDialog.setFilterPath("C:/Users");
String selectedDirectory = directoryDialog.open();

Following is the complete working application.
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  DirectoryDialog directoryDialog = new DirectoryDialog(shell);
  directoryDialog.setText("DirectoryDialog Demo");
  directoryDialog.setFilterPath("C:/Users");
  String selectedDirectory = directoryDialog.open(); 
  
  System.out.println("User selected : " + selectedDirectory);
  shell.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(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 window.



Previous                                                 Next                                                 Home