Sunday 25 December 2016

SWT: Disabling (or) enabling menu items

By using ‘setEnabled’ method of MenuItem class, you can enable (or) disable menu items. Following is the definition of ‘setEnabled’ method from JavaDoc.

public void setEnabled (boolean enabled)
Enables the receiver if the argument is true, and disables it otherwise. A disabled menu item is typically not selectable from the user interface and draws with an inactive or "grayed" look.

Ex
openFileItem.setEnabled(false);

Above statement disables menu item.
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');
  openFileItem.setEnabled(false);

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

 }
}



As you see above figure, ‘Open File’ option is disabled.



Previous                                                 Next                                                 Home

No comments:

Post a Comment