When the user clicks on the icon, nothing happens. That's because there is not a registered listener on TrayItem
itself. There are two listeners that can be registered: a SelectionListener
, called when the icon is clicked, and a MenuDetectListener
which can respond to a context-sensitive menu. The former will be used to present a clock in its own window, which in SWT terms is called a Shell
.
Activator
class.Shell
:private Shell shell;
run()
method inside the Runnable
in the Activator
class's start()
method.TrayItem
, call addSelectionListener()
, which creates a new Shell
with a ClockView
:trayItem.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { if (shell == null) { shell = new Shell(trayItem.getDisplay()); shell.setLayout(new FillLayout()); new ClockWidget(shell, SWT.NONE, new RGB(255, 0, 255)); shell.pack(); } shell.open(); } });
Shell
is disposed when the bundle is stopped, so add the following to the end of the Activator
class 's stop()
method:if (shell != null && !shell.isDisposed()) { Display.getDefault().asyncExec(new Runnable() { public void run() { if (shell != null && !shell.isDisposed()) shell.dispose(); } }); }
TrayItem
, and use the host OSGi console to stop and start the bundle. The window should disappear when the bundle is stopped.When TrayItem
is installed into the system's Tray
, event listeners can be registered to respond to user input. The one that gets called when the icon is clicked is the SelectionListener
, and this gives the opportunity to display the window (or Shell
in SWT's terminology).
The Display
associated with the TrayItem
is used when instantiating the Shell
. Although either Display.getDefault()
or Display.getCurrent()
could be used, neither of these would be the right option. When developers are running in multi-monitor mode, or with a virtual display that spans multiple desktops, it's important to ensure that the Shell
is shown on the same display as the corresponding Tray
.
Without a LayoutManager
, the clock won't show up. FillLayout
is used here to ensure that the clock is made as large as the window (and resizes accordingly to the window itself). Once the window is created the pack()
method is called, which sets the size of the window to the preferred size of its children; in this case, ClockView
.
Finally, the window is shown with the open()
call.