TrayIcon: AWT or Swing

Greetings,

I played a bit with the SystemTray and the TrayIcon this morning and I

noticed something strange. Have a look at an early document that

describes this new feature:

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

... and have a close look at those MenuItems: they're definitely Swing

JMenuItems (AWT MenuItems can't display Icons nor Images), but

when I read the API for the TrayIcon, it only manages a PopupMenu

which is an AWT component and can't handle JMenuItems.

IOW that entire tray facility is AWT based. Or am I missing something?

kind regards,

Jos

[676 byte] By [JosAHa] at [2007-11-26 22:16:15]
# 1

Sad but true.

There are some ways to show JPopupMenu in tray:

1) using org.jdesktop.jdic.tray.TrayIcon

2) http://jeans.studentenweb.org/java/trayicon/trayicon.html

3) http://systray.sourceforge.net

4) my ;-)

class JTrayIcon extends TrayIcon {

static AWTEventListener globalEventListener = null;

private JPopupMenu popupMenu = null;

public JTrayIcon(ImageIcon icon, String hint, JPopupMenu apopupMenu) {

super(icon.getImage(), hint);

initPopupMenu();

setPopupMenu(apopupMenu);

}

private Window getParentWindow(Container c) {

while (c != null) {

c = c.getParent();

if (c instanceof Window)

return (Window) c;

}

return null;

}

private void initPopupMenu() {

if (globalEventListener == null) {

globalEventListener = new AWTEventListener() {

public void eventDispatched(AWTEvent event) {

if (event.getSource() instanceof TrayIcon) {}

event.setSource(null); // needs to make happy BasicPopupMenuIU's AWTEventListener

}

};

Toolkit.getDefaultToolkit().addAWTEventListener(globalEventListener,

AWTEvent.MOUSE_EVENT_MASK);

}

addMouseListener(new MouseListener() {

public void mouseClicked(MouseEvent e) {

e.setSource(null);

if (popupMenu == null)

return;

switch (e.getButton()) {

case MouseEvent.BUTTON1:

if (e.getClickCount() > 1) {

Component c = popupMenu.getComponent(0);

if (c instanceof AbstractButton)

((AbstractButton)c).doClick();

}

break;

case MouseEvent.BUTTON3:

popupMenu.show(null, e.getX(), e.getY());

Window w = getParentWindow(popupMenu);

w.setAlwaysOnTop(true);

}

}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

});

}

public void setPopupMenu(JPopupMenu apopupMenu) {

popupMenu = apopupMenu;

}

}

vk65535a at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...
# 2
Jos - you stealth thread-bumper you
KelVarnsona at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...
# 3

Sorry, forgot this:

if (event.getSource() instanceof TrayIcon) {}

need to be changed:

if (event.getSource() instanceof TrayIcon)

vk65535a at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Jos - you stealth thread-bumper you

Am not! I've hardly been around here the last couple of days (too busy).

First of all @vk(64K-1): thank you for your reply. For the moment I just

don't use Sun's crappy implementation of that system tray; I'll have a

look at your links and suggestions; thank you very much. My plans were

to just wait a bit 'till Sun comes up with something decent. Their current

implementation is just too limited (just AWT, no Swing, that's a nono)

and I've put any system tray usage quite low on my TODO list.

Second, @kelvarnson: :-P

kind regards,

Jos ;-)

JosAHa at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...
# 5

> > Jos - you stealth thread-bumper you

>

> Am not! I've hardly been around here the last couple of days (too busy).

Fair enough. It's just that I noticed the thread mysteriously creeping to the top of the heap about 3 or 4 times yesterday* without any new replies, and then again today. Perhaps vk was re-re-re-editing his post.

* - almost spelled this as "yeasterday", which begs the question: could this word be used legitimately next Monday?

KelVarnsona at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...
# 6

> > > Jos - you stealth thread-bumper you

> >

> > Am not! I've hardly been around here the last couple of days (too busy).

>

> Fair enough. It's just that I noticed the thread mysteriously creeping to

> the top of the heap about 3 or 4 times yesterday* without any new

> replies, and then again today. Perhaps vk was re-re-re-editing

> his post.

't wasn't me Your Honour; 't must've been vk: he was posting all that

code; I didn't do nothin' squire!

> * - almost spelled this as "yeasterday", which begs the question:

> could this word be used legitimately next Monday?

It could, but it's already officially named "Happy Hangover Day!"

kind regards,

Jos ;-)

JosAHa at 2007-7-10 11:09:17 > top of Java-index,Desktop,Core GUI APIs...