Friday, 23 December 2016

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

No comments:

Post a Comment