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.
Related
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);
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.
i am working on a java project and i want to display a message in popup like the popup of "Safe To Remove Hardware" occurred in the windows when we click on the Eject icon for USB Drives.
I want show my message in the same kind of popup using java code.
Use the SystemTray class.
To create an icon with a tooltip, use something like this:
SystemTray tray = SystemTray.getSystemTray();
TrayIcon icon = new TrayIcon(....);
icon.setToolTip("I have finished my work");
icon.setActionListener(this);
tray.add(trayIcon);
Then in the class that displays the tooltip, implement the ActionListener interface to be informed when the user clicks on the icon and/or the tooltip (that's what the setActionListener() is for)
For more details refer to the Javadocs of SystemTray, TrayIcon and ActionListener
You simply need to use the displayMessage(...) method of the TrayIcon class.
Try your hands on this code, is this what you wanted :
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class BalloonExample
{
private void createAndDisplayGUI()
{
TrayIcon trayIcon = new TrayIcon(createImage(
"/image/caIcon.png", "tray icon"));
SystemTray tray = SystemTray.getSystemTray();
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.out.println("TrayIcon could not be added.");
return;
}
trayIcon.displayMessage("Balloon", "My First Balloon", TrayIcon.MessageType.INFO);
}
//Obtain the image URL
protected static Image createImage(String path, String description) {
URL imageURL = BalloonExample.class.getResource(path);
if (imageURL == null) {
System.err.println("Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new BalloonExample().createAndDisplayGUI();
}
});
}
}
Have a look at my question here. Basically that tooltip is a Balloon tip and you can use ShellNotifyIcon to create one.
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/
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/>