system tray icon not displaying on startup of program - java

I used following code for display system tray icon and message. Startup message and tooltip message is displayed well. But tray icon is not displaying. The icon is in images folder. How can i solve this problem ?
public void systemTray() {
try {
SystemTray tray = SystemTray.getSystemTray();
ImageIcon icon=new ImageIcon(getClass().getResource("/images/Reg Member MO.png"));
Image image = icon.getImage();
TrayIcon icn = new TrayIcon(image, "This is demonstration system tray");
icn.setToolTip("Now you can see system tray\ntooltip here\nThis is demonstration system tray ToolTip");
tray.add(icn);
icn.displayMessage("This is demonstration System Tray message", "You can add some text to\ndisplay here as System Tray Message", TrayIcon.MessageType.INFO);
} catch (AWTException ex) {
Logger.getLogger(StartupSystemTray.class.getName()).log(Level.SEVERE, null, ex);
}
}

May be your icon image is too large than allocated space. Try using public void setImageAutoSize(boolean autosize) method as icn.setImageAutoSize(true). This will re-size the image.

Related

Why isn't the popup menu for my System Tray Icon responding to inputs?

I've been working on a background java program for a while and its almost ready to be released so I thought I should probably add a way to exit the program. I have this function:
private static void setupSysTray() {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
try {
final PopupMenu popupMenu = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File(workingDirectory +
fileSeparator + "tray.png")), "Multi");
final SystemTray tray = SystemTray.getSystemTray();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(e -> {
System.out.println("Something happened!");
System.exit(0);
});
popupMenu.add(exitItem);
trayIcon.setPopupMenu(popupMenu);
tray.add(trayIcon);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
I presumed this would handle it, I pieced it together from various stack overflow posts and the offical documentation. The result is that the tray icon appears, with the correct image and tooltip. When I right click it I see the menu item for "exit" show up. But that's where it breaks, the menu item doesn't have any hover coloring (leading me to believe input at all with it is broken) and clicking on the item turns up no results. Have I made some silly mistake like ordering the adding of items wrong? What's going on here?
As it turns out I was making use of a Global Mouse Hook in an old part of the code that I had all but forgotten about. The hook is from this repository and the fix was changing the hook's raw input setting from true to false.
// True instead of false seems to block JavaFX events
private static GlobalMouseHook mouseHook = new GlobalMouseHook(false);

How to set system-tray icon using only AWT

I've created a calculator using AWT Frames. I wanna know how to add a tray-icon to my Cal. I can only use AWT not Swing.
final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage("pathToImage"));
final SystemTray tray = SystemTray.getSystemTray();
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}
Found at this page http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html
You should also check if SystemTray is supported before using the following snippet
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
//..
}
Note that SystemTray is from package java.awt as per your request.

Change System tray Icon in java

I just want to change the system tray Icon image for my application. I did 2 things -
Just changed the URL in the default program -
final TrayIcon trayIcon = new TrayIcon(createImage("images/Graph.png", "tray icon"));
Second try -
Image img = Toolkit.getDefaultToolkit().getImage("images/Graph.png");
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
The application launches in both cases but no image is shown. Its a blank placeholder. What am i doing wrong ?
images/Graph.png is not a valid URL for an image located in your jar. Hence, I guess that img is null on your second try.
I suggest you this way :
//Get the URL with method class.getResource("/path/to/image.png")
URL url = System.class.getResource("/images/Graph.png");
//Use it to get the image
Image img = Toolkit.getDefaultToolkit().getImage(url);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
You shall also ensure that images/ is in your classpath.
The problem is the way you include the image file as the image is inside your . jar, use getResource() or getResourceAsStream, try this:
try {
InputStream inputStream= ClassLoader.getSystemClassLoader().getResourceAsStream("/images/Graph.png");
//or getResourceAsStream("/images/Graph.png"); also returns inputstream
BufferedImage img = ImageIO.read(inputStream);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
}
catch (IOException e) {}
For me works in the next way:
// URL: MyProject/src/MyGUI/check.png
// Small icon on secondary taskbar
Image image= ImageIO.read(getClass().getResource("/MyGUI/check.png"));
trayIcon = new TrayIcon(image, "App name", popup);
trayIcon.setImageAutoSize(true);
Results

How to show icon file in system tray?

I am adding my application in system tray when I close it. But it is not displaying the icon. When I try to show picture file then it works fine but when I try with an icon file it does not work. How can I display an icon instead picture?
Image image = new ImageIcon("src/resources/busylogo.jpg").getImage();
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
Better use Toolkit to load an icon.
That's a low size file, and an asynchronous load will give you less problems.
Try this code, which is recommanded by Sun.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
tray.add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
More informations here : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

SystemTray based application without window on Mac OS X

How do I do an application that runs only as a SystemTray TrayIcon on mac os x, (without an awt window and dock icon)?
The code I'm using is this:
public class App
{
public static void main( String[] args )
{
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
trayIcon = new TrayIcon(image, "Tray Demo");
trayIcon.setImageAutoSize(true);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
System.out.println("Tray is not supported");
// System Tray is not supported
}
}
}
The problem is I'm getting a dock icon with title com.cc.ew.App
To prevent icon in dock, you must in <your-app>-Info.plist file add boolean key LSUIElement and set it to YES.
<key>LSUIElement</key>
<true/>

Categories

Resources