SWT Tray Demo: Right-click menu, click Hide, examples of all events

Source: Internet
Author: User
Tags window
Menu
SWT's system tray can handle only three kinds of events: left-click, double-click and right-click, right click this is what I see SWT 3.0.1 Win32 Source code known this demo has the following functions: 1. When left click, Hide, Show main Window 2. Right-click, pop-up menu Note: 1. Double-click and right-click is an event that cannot differentiate between 2. The left-click double-click will produce the following event: left-clicking, double-click, left-click 3. For pop-up menus, menu must have a parent, and if there is no shell in the program, You can build an invisible shell specifically as the parent of the menu.

Import Org.eclipse.swt.*;import Org.eclipse.swt.widgets.*;import Org.eclipse.swt.events.*;import Org.eclipse.swt.layout.filllayout;import Org.eclipse.swt.graphics.image;class HelloTray Implements Selectionlistener{display Display; Shell Shell; Button Hello; Tray Tray; Trayitem ti; Menu menu; MenuItem Mi1; MenuItem mi2; MenuItem quit;public Hellotray () {display = Display.getdefault (); shell = new Shell (); Filllayout filllayout = new Filllayout (); filllayout.type = SWT. Vertical;shell.setlayout (filllayout); hello = new Button (Shell, SWT. NONE); Hello.settext ("Hello world!");    Hello.addselectionlistener (New Selectionadapter () {public void widgetselected (Selectionevent e) {        if (Hello.gettext (). Equals ("Hello world!"))          hello.settext ("clicked");       else         hello.settext ("Hello world!");}); Menu=new Menu (shell); mi1=new MenuItem (MENU,SWT. PUSH); Mi1.settext ("MenuItem 1"); Mi1.addselectionlistener (this); Mi2=new MenuItem (MENU,SWT. PUSH); Mi2.settext ("MenuItem 2"); Mi2.addselectionlistener (this); Quit=new MenuItem (MENU,SWT. PUSH); Quit.settext ("Quit"); Quit.addselectionlistener (this);//Generate SWT Traytray=display.getsystemtray (); ti=new Trayitem (tray,0); Ti.settooltiptext ("This is a SWT tray!"); Ti.setimage (New Image (Display, "E:\\my documents\\my pictures\\alm.gif");//swt,tray All events: Ti.addselectionlistener ( New Selectionlistener () {//Left click public void widgetselected (Selectionevent e) {System.out.println ("Tray selcted"); When left click, Hide, Show main window if (shell.isvisible ()) {shell.setvisible (false);} Else{shell.setvisible (True); Shell.forceactive ();}} Left-click, right-click, it is public void widgetdefaultselected (Selectionevent e) {System.out.println ("Tray widgetdefaultselected" );}});/ /Right-click, pop-up menu Ti.addlistener (SWT. Menudetect,new Listener () {public void Handleevent (event event) {System.out.println ("SWT. Menudetect "); Menu.setlocation (Display.getcursorlocation ());        menu.setvisible (True);}); public void Start () {Shell.open (), while (!shell.isdisposed ()) {if (!display.readanddispatch ()) Display.sleep ();} Display.dispose ();} public static void Main (string[] args) {Hellotray app=new hellotray (); App.start ();} public void widgetselected (Selectionevent e) {System.out.println (E.getsource () + "selected."); if (E.getsource () ==quit) {System.out.println ("quit");d Isplay.dispose (); System.exit (0);}} public void widgetdefaultselected (Selectionevent e) {}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.