Languages
 
 
 
 
Follow Me



 
 

Java application menu in system tray

  Tagged

 I got the task to implement system tray menu for java desktop application. After some research I found several ways to finish my task:

  • java 6
  • java desktop components integraion library (jdic)
  • eclipse swt

 I have only the one biggest requirement for new application. It should support all most popular operating systems (win, linux and mac os). Java 6 has no support on mac os x. For some reason I was thinking that Jdic has no support on Mac OS X too (now I know that I was wrong). Starting from 3.3 version, SWT started supporting all main operation systems. And as the result SWT became my one and only choice.

 After I've written some peace of code, I've found following problem with SWT: tray menu has wrong position on screen relatively to the tray icon. It's easy to reproduce bug using Azureus, which also uses SWT:

 If you would compare these examples with trays from native Mac OS X application, you would see correct alignment of the tray menu:

 In application code I found only way to align 'y' coordinate (heaving in mind that menu height is constant):

package com.kobyleha.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class MacTrayTest {

	private static final String MENU_ITEM_TEXT = "Menu Item";

	private static final String ICON_IMAGE = "icon.gif";

	public static void main(String[] args) {
		MacTrayTest test = new MacTrayTest();
		test.showTray();
	}

	public void showTray() {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		Tray tray = display.getSystemTray();
		if (tray != null) {
			initTray(display, shell, tray);
		}
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();		
	}
	
	private void initTray(final Display display, final Shell shell, Tray tray) {
		Image image = new Image(display, MacTrayTest.class.getClassLoader()
				.getResourceAsStream(ICON_IMAGE));
		TrayItem item = new TrayItem(tray, SWT.NONE);
		item.setImage(image);
		final Menu menu = new Menu(shell, SWT.POP_UP);
		MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setText(MENU_ITEM_TEXT);

		item.addSelectionListener(getSelectionListener(display, menu));
		item.addMenuDetectListener(getMenuDetectListener(display, menu));
	}

	private MenuDetectListener getMenuDetectListener(final Display display, final Menu menu) {
		return new MenuDetectListener() {
			public void menuDetected(MenuDetectEvent arg0) {
				menu.setLocation(display.getCursorLocation().x - 13, 26);
				menu.setVisible(true);
			}
		};
	}

	private SelectionListener getSelectionListener(final Display display, final Menu menu) {
		return new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent event) {
				widgetSelected(event);
			}
			public void widgetSelected(SelectionEvent event) {
				menu.setLocation(display.getCursorLocation().x - 13, 26);
				menu.setVisible(true);
			}
		};
	}
}

 If someone knows how to align 'x' coordinate - you are welcome (:

Share this

burberry bags onlineSimply transported from one pa

There are fleece blankets, plush blankets, custom blankets, embroidered blankets and even knitted blankets. It just has the name of the first inventor who was Klaus Marten. The animals were fed a high fat diet accompanied with a green tea made from 1.1 to 2.0 percent green tea extract. I had a difficult time in narrowing down my choices to just a handful of these stunning dresses. Another major variety offered in terms of cushion type is the open cell foam that is an absorbent model. Although the main purpose of lighting fixtures is to provide light burberry coin purse, they offer a lot more. The BURBERRY BRIT BY BURBERRYS EAU DE PARFUM also has some tinch of lime, icy pear & green almond. Diverse organizations discount burberry handbags, such as Catsplay, also supply identical cupboard models. Any divides each of the 8 directions (4 main and 4 intermediate) into three sectors burberry bags online, which make a total of 24 directions.. Obviously the most crucial benefit to help choosing that ear bud style of headphone is likely small and very mobile around together with you. These animals like to sleep in quiet areas, as well. The total portable charcoal grill shouldn't end up deficient in anything. If this circuit uses more, they can increase L1 and L2's circle number, improve the L1 and L2 than circle number, VT DD13005 etc to three more power tube, VD and C for higher compression, the power supply voltage up to 9 v ~ 12 v, the improved circuit can be used as a simple dc high voltage generator. Candles also have a distinctive aroma that often blends with that of the wax or oil, yielding a less attractive scent. Nike golf balls for the novices The price of Nike golf balls is rather high. These Asterix albums are about the fun-filled adventures taking place in the time of Gaul and the rule of Caesar. Why would they offer one thing at a discount that they could very easily get for full retail worth? This is a large tip off. Shop safe!. Marshal Bo Asi firmly believe that the new season, Torres can restore its former glory, but the process is not so smug, until the game against Manchester United back El Nino was the feeling of scoring, but the last round of league Torres broke again, so that fans can not help but expectations have become more promise.. The ring in the marriage binds two partners for all time besides all problems and situation. When you produce your individual perfume you get to work with various wonderful scents and the consequence could be extremely beneficial. and God knows what their money could do for these people/children with these illnesses or disabilities.Related articles:

Post new comment

CAPTCHA
Are You Human/Bot?
 
© 2006-2010 kobyleha.com