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

No comments:

Post a Comment