Java application menu in system tray
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 (:
»
- 3806 reads
- Russian




Post new comment