Friday, 23 December 2016

SWT: Add listeners to Menu

By using ‘MenuListener’, you can get notifications, whenever menu is shown (or) hidden. ‘MenuListener’ interface provides following methods.

Method
Description
public void menuHidden(MenuEvent e)
Notify when menu is hidden.
public void menuShown(MenuEvent e)
Notify when menu is shown.

fileMenu.addMenuListener(new MenuListener() {
         @Override
         public void menuShown(MenuEvent e) {
                 System.out.println("SubMenu shown");
         }

         @Override
         public void menuHidden(MenuEvent e) {
                 System.out.println("SubMenu hidden");
         }
});


Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

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

  Menu menu = new Menu(shell, SWT.BAR);
  shell.setMenuBar(menu);

  MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
  fileMenuItem.setText("File");
  // SWT.ALT is not working, but MOD1 is working
  fileMenuItem.setAccelerator(SWT.MOD1 + 'F');

  MenuItem edit = new MenuItem(menu, SWT.CASCADE);
  edit.setText("Edit");

  MenuItem source = new MenuItem(menu, SWT.CASCADE);
  source.setText("Source");

  addSubMenuToFile(shell, fileMenuItem);

  /* Add Action Listeners */
  fileMenuItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("fileMenuItem item is clicked");
   }
  });

 }

 private static void addSubMenuToFile(Shell shell, MenuItem fileMenuItem) {

  Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
  fileMenuItem.setMenu(fileMenu);

  MenuItem newFileItem = new MenuItem(fileMenu, SWT.PUSH);
  newFileItem.setText("New (CTRL + n)");
  newFileItem.setAccelerator(SWT.CTRL + 'n');

  MenuItem openFileItem = new MenuItem(fileMenu, SWT.PUSH);
  openFileItem.setText("Open File (CTRL + f)");
  openFileItem.setAccelerator(SWT.CTRL + 'o');

  /* Add Separator */
  MenuItem separator1 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Define switch workspace menu */
  MenuItem switchWorkSpaceItem = new MenuItem(fileMenu, SWT.CASCADE);
  switchWorkSpaceItem.setText("Switch Workspace");
  switchWorkSpaceItem.setAccelerator(SWT.MOD1 + 'w');

  Menu switchWorkSpacemenu = new Menu(shell, SWT.DROP_DOWN);
  switchWorkSpaceItem.setMenu(switchWorkSpacemenu);

  MenuItem prevWorkSpaceItem = new MenuItem(switchWorkSpacemenu, SWT.PUSH);
  prevWorkSpaceItem.setText("Previous Workspace");

  /* Add Separator */
  MenuItem separator2 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Adding check boxes and radio buttons */
  MenuItem radioItem = new MenuItem(fileMenu, SWT.RADIO);
  radioItem.setText("Radio");

  MenuItem checkItem = new MenuItem(fileMenu, SWT.CHECK);
  checkItem.setText("Check");

  /* Add Action Listeners */
  newFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("new file item is clicked");
   }
  });

  openFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("Open file item is clicked");
   }
  });

  radioItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Radio item toggled to:" + radioItem.getSelection());
   }
  });
  checkItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Check item toggled to:" + checkItem.getSelection());
   }
  });

  fileMenu.addMenuListener(new MenuListener() {
   @Override
   public void menuShown(MenuEvent e) {
    System.out.println("SubMenu shown");
   }

   @Override
   public void menuHidden(MenuEvent e) {
    System.out.println("SubMenu hidden");
   }
  });

 }

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

  addWidgetsToShell(display, shell);

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

  /*
   * 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: Adding Accelerators to menu items (Key board short cuts)

Accelerators are used to give keyboard short cuts like ALT + F, CTRL + N etc., An accelerator is the bit-wise OR of zero or more modifier masks and a key.

Examples:
SWT.MOD1 | SWT.MOD2 | 'T', SWT.MOD3 | SWT.F2
SWT.CONTROL | SWT.SHIFT | 'T',
SWT.ALT | SWT.F2

The default value is zero, indicating that the menu item does not have an accelerator.

Following snippet adds short cut ALT + F to the file menu.

MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
fileMenuItem.setText("File");
// SWT.ALT is not working, but MOD1 is working
fileMenuItem.setAccelerator(SWT.MOD1 + 'F');

Following is the complete working example.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

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

  Menu menu = new Menu(shell, SWT.BAR);
  shell.setMenuBar(menu);

  MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
  fileMenuItem.setText("File");
  // SWT.ALT is not working, but MOD1 is working
  fileMenuItem.setAccelerator(SWT.MOD1 + 'F');

  MenuItem edit = new MenuItem(menu, SWT.CASCADE);
  edit.setText("Edit");

  MenuItem source = new MenuItem(menu, SWT.CASCADE);
  source.setText("Source");

  addSubMenuToFile(shell, fileMenuItem);

  /* Add Action Listeners */
  fileMenuItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("fileMenuItem item is clicked");
   }
  });

 }

 private static void addSubMenuToFile(Shell shell, MenuItem fileMenuItem) {

  Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
  fileMenuItem.setMenu(fileMenu);

  MenuItem newFileItem = new MenuItem(fileMenu, SWT.PUSH);
  newFileItem.setText("New (CTRL + n)");
  newFileItem.setAccelerator(SWT.CTRL + 'n');

  MenuItem openFileItem = new MenuItem(fileMenu, SWT.PUSH);
  openFileItem.setText("Open File (CTRL + f)");
  openFileItem.setAccelerator(SWT.CTRL + 'o');

  /* Add Separator */
  MenuItem separator1 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Define switch workspace menu */
  MenuItem switchWorkSpaceItem = new MenuItem(fileMenu, SWT.CASCADE);
  switchWorkSpaceItem.setText("Switch Workspace");
  switchWorkSpaceItem.setAccelerator(SWT.MOD1 + 'w');

  Menu switchWorkSpacemenu = new Menu(shell, SWT.DROP_DOWN);
  switchWorkSpaceItem.setMenu(switchWorkSpacemenu);

  MenuItem prevWorkSpaceItem = new MenuItem(switchWorkSpacemenu, SWT.PUSH);
  prevWorkSpaceItem.setText("Previous Workspace");

  /* Add Separator */
  MenuItem separator2 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Adding check boxes and radio buttons */
  MenuItem radioItem = new MenuItem(fileMenu, SWT.RADIO);
  radioItem.setText("Radio");

  MenuItem checkItem = new MenuItem(fileMenu, SWT.CHECK);
  checkItem.setText("Check");

  /* Add Action Listeners */
  newFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("new file item is clicked");
   }
  });

  openFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("Open file item is clicked");
   }
  });

  radioItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Radio item toggled to:" + radioItem.getSelection());
   }
  });
  checkItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Check item toggled to:" + checkItem.getSelection());
   }
  });

 }

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

  addWidgetsToShell(display, shell);

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

  /*
   * 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

Wednesday, 21 December 2016

SWT: Menu and MenuItem widgets

In this post, I am going to explain how to create Menu bar.

Following statement is used to create Menu.
Menu menu = new Menu(shell, SWT.BAR);

Following statement is used to set the menu bar at the top of shell.
shell.setMenuBar(menu);

How to add menu items to the menu bar?
By using MenuItem class, you can add a menu item to menu bar. Following statement adds menu item ‘fileMenuItem’ to the menu bar ‘menu’.

MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
fileMenuItem.setText("File");

Make sure that the style constant is SWT.CASCADE, otherwise you can’t attach a drop-down menu to it.

Let me add drop down to the file menu item. When user clicks on file menu item, he can able to see the options like ‘New’, ‘Open’ etc., First we need to create a drop down menu and attach it to the file menu item. Note that the style constant for the fileMenu must be DROP_DOWN.

Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileMenuItem.setMenu(fileMenu);

MenuItem newFileItem = new MenuItem(fileMenu, SWT.PUSH);
newFileItem.setText("New");

MenuItem openFileItem = new MenuItem(fileMenu, SWT.PUSH);
openFileItem.setText("Open File");

Now, you can add action listeners to newFileItem, openFileItem.

newFileItem.addListener(SWT.Selection, new Listener() {
         @Override
         public void handleEvent(Event event) {
                 System.out.println("new file item is clicked");
         }
});

Following table summarizes different style options available with MenuItem.

Style
Description
CHECK
Style constant for check box behavior
CASCADE
Style constant for cascade behavior
PUSH
Style constant for push button behavior
RADIO
Style constant for radio button behavior
SEPARATOR
Style constant for line separator behavior

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

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

  Menu menu = new Menu(shell, SWT.BAR);
  shell.setMenuBar(menu);

  MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
  fileMenuItem.setText("File");

  MenuItem edit = new MenuItem(menu, SWT.CASCADE);
  edit.setText("Edit");

  MenuItem source = new MenuItem(menu, SWT.CASCADE);
  source.setText("Source");

  addSubMenuToFile(shell, fileMenuItem);

 }

 private static void addSubMenuToFile(Shell shell, MenuItem fileMenuItem) {

  Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
  fileMenuItem.setMenu(fileMenu);

  MenuItem newFileItem = new MenuItem(fileMenu, SWT.PUSH);
  newFileItem.setText("New");

  MenuItem openFileItem = new MenuItem(fileMenu, SWT.PUSH);
  openFileItem.setText("Open File");

  /* Add Separator */
  MenuItem separator1 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Define switch workspace menu */
  MenuItem switchWorkSpaceItem = new MenuItem(fileMenu, SWT.CASCADE);
  switchWorkSpaceItem.setText("Switch Workspace");

  Menu switchWorkSpacemenu = new Menu(shell, SWT.DROP_DOWN);
  switchWorkSpaceItem.setMenu(switchWorkSpacemenu);

  MenuItem prevWorkSpaceItem = new MenuItem(switchWorkSpacemenu, SWT.PUSH);
  prevWorkSpaceItem.setText("Previous Workspace");

  /* Add Separator */
  MenuItem separator2 = new MenuItem(fileMenu, SWT.SEPARATOR);

  /* Adding check boxes and radio buttons */
  MenuItem radioItem = new MenuItem(fileMenu, SWT.RADIO);
  radioItem.setText("Radio");

  MenuItem checkItem = new MenuItem(fileMenu, SWT.CHECK);
  checkItem.setText("Check");

  /* Add Action Listeners */
  newFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("new file item is clicked");
   }
  });

  openFileItem.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    System.out.println("Open file item is clicked");
   }
  });

  radioItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Radio item toggled to:" + radioItem.getSelection());
   }
  });
  checkItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("Check item toggled to:" + checkItem.getSelection());
   }
  });

 }

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

  addWidgetsToShell(display, shell);

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

  /*
   * 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

Sunday, 18 December 2016

SWT: Progress Bar widget

You can define Progress bar widget using ProgressBar class.

Following statements define horizontal progress bar.

ProgressBar horizontalProgressBar = new ProgressBar(shell, SWT.HORIZONTAL);
horizontalProgressBar.setMaximum(100);
horizontalProgressBar.setMinimum(10);
horizontalProgressBar.setBounds(50, 50, horizontalScaleWidth, horizontalScaleHeight);
horizontalProgressBar.setBackground(new Color(display, 255, 255, 255));
horizontalProgressBar.setSelection(30);

Following statements define vertical progress bar.

ProgressBar verticalProgressBar = new ProgressBar(shell, SWT.VERTICAL);
verticalProgressBar.setMaximum(1000);
verticalProgressBar.setMinimum(500);
verticalProgressBar.setBounds(250, 150, verticalScaleWidth, verticalScaleHeight);
verticalProgressBar.setBackground(new Color(display, 255, 255, 255));
verticalProgressBar.setSelection(650);

Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

 private static int horizontalScaleWidth = 600;
 private static int horizontalScaleHeight = 50;

 private static int verticalScaleWidth = 50;
 private static int verticalScaleHeight = 400;

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

  ProgressBar horizontalProgressBar = new ProgressBar(shell, SWT.HORIZONTAL);
  horizontalProgressBar.setMaximum(100);
  horizontalProgressBar.setMinimum(10);
  horizontalProgressBar.setBounds(50, 50, horizontalScaleWidth, horizontalScaleHeight);
  horizontalProgressBar.setBackground(new Color(display, 255, 255, 255));
  horizontalProgressBar.setSelection(30);

  ProgressBar verticalProgressBar = new ProgressBar(shell, SWT.VERTICAL);
  verticalProgressBar.setMaximum(1000);
  verticalProgressBar.setMinimum(500);
  verticalProgressBar.setBounds(250, 150, verticalScaleWidth, verticalScaleHeight);
  verticalProgressBar.setBackground(new Color(display, 255, 255, 255));
  verticalProgressBar.setSelection(650);

 }

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

  addWidgetsToShell(display, shell);

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

  /*
   * 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