Search |
||
Using JPopupMenu in TrayIconPosted by ixmal on May 4, 2006 at 5:23 AM PDT
What's the problem?Tray icons introduced in Mustang have several properties and methods corresponding to image for the icon, tooltip text, popup menu and ability to show some message to the user. Let's inspect the popup menu more closely. Popup menu used in tray icons must be an instance of java.awt.PopupMenu. This class represents a menu and allows to insert or delete simple menu items and separators. You cannot change its appearance or even provide an image for particular menu item.Use JPopupMenu insteadThere is another class to create popup menus provided by Swing: javax.swing.JPoupMenu. As all Swing components this class can be customized in any way and makes use of the current Look&Feel. Here is a short illustrating example how it can be used:First, create a tray icon with a null popup since we're going to use our own one:
TrayIcon trayIcon = new TrayIcon(someImage, "Tooltip", null);
Then, create a custom JPopupMenu:
JPopupMenu jpopup = new JPopupMenu();
JMenuItem javaCupMI = new JMenuItem("Example", new ImageIcon("javacup.gif"));
jpopup.add(javaCupMI);
jpopup.addSeparator();
JMenuItem exitMI = new JMenuItem("Exit");
jpopup.add(exitMI);
At last, show the popup menu when user clicks a right mouse button on the tray icon:
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpopup.setLocation(e.getX(), e.getY());
jpopup.setInvoker(jpopup);
jpopup.setVisible(true);
}
}
});
And enjoy the result: ![]() Magic?You may probably ask me: "What is that magic line 'jpopup.setInvoker(jpopup);' for?" The answer is not as simple as quiestion is, though. JavaDoc for JPopupMenu.setInvoker() method says that invoker is the component in which the popup menu menu is to be displayed. This property can be set to null, but it leads to popup menu to operate incorrectly.Setting popup's invoker to itself has some side-effect too. Usually, if you dispose all the windows in your application, Java exits automatically. If you show JPopupMenu with self invoker, it won't, so the only way to terminate the whole application is to call to System.exit().Both of these issues are well known to Swing team and will be corrected before the Mustang final release. See 6421284 and 6400183 for details. »
Related Topics >>
Java Desktop Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|