Sunday, 25 December 2016

SWT: Popup menus

You can create popup menus, using the style SWT.POP_UP and Menu constructor.

Ex:
Menu popupmenu = new Menu(shell, SWT.POP_UP);

You can attach pop up menu to any SWT widget and whenever you right click on the widget, you can able to see the pop-up menu associated with it.
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 shellPopUpMenu = new Menu(shell, SWT.POP_UP);
  MenuItem viewItem = new MenuItem(shellPopUpMenu, SWT.PUSH);
  viewItem.setText("View");

  MenuItem sortByItem = new MenuItem(shellPopUpMenu, SWT.PUSH);
  sortByItem.setText("Sort By");

  MenuItem groupByItem = new MenuItem(shellPopUpMenu, SWT.PUSH);
  groupByItem.setText("Group By");

  MenuItem refreshItem = new MenuItem(shellPopUpMenu, SWT.PUSH);
  refreshItem.setText("Refresh");

  MenuItem separator = new MenuItem(shellPopUpMenu, SWT.SEPARATOR);

  MenuItem customizeFolderItem = new MenuItem(shellPopUpMenu, SWT.PUSH);
  customizeFolderItem.setText("Customize this Folder");

  viewItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("view item clicked");
   }
  });

  sortByItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("sortByItem clicked");
   }
  });

  groupByItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("groupByItem clicked");
   }
  });

  refreshItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    System.out.println("refreshItem clicked");
   }
  });

  shell.setMenu(shellPopUpMenu);
 }

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

 }
}

Run above application, right click the shell window, you can able to see popup menu.



Previous                                                 Next                                                 Home

No comments:

Post a Comment