SystemTray based application without window on Mac OS X - java

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/>

Related

system tray icon not displaying on startup of program

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.

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.

What kind of popup "Safe To Remove Hardware" in Windows

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.

How to add application to System Tray

I want to add my application to the system tray when it's window is closed (similar to the Google Talk application). And then when I click the on icon in the system tray the application window becomes active again. How can I do this in Java?
final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("images.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
this.addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == EXIT_ON_CLOSE) {
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
setVisible(true);
}
});
setVisible(false);
}
}
});
you have set DefaultCloseOperations correctly
myFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
this code line is same as myFrame.setVisible(false), then for restore of JFrame from JPopupMenu to call only myFrame.setVisible(true)
I got answer. Now when i close window its closing and when i click on System tray icon then it again open my window
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/ChatIcon1.jpeg");
final TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setToolTip("OfficeCommunicator");
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
trayIcon.displayMessage("hi", "You Opened Me Again", TrayIcon.MessageType.INFO);
setVisible(true);
}
});
}

How do I put a Java app in the system tray?

I have a little control-panel, just a little application that I made. I would like to minimize/put the control-panel up/down with the systemicons, together with battery life, date, networks etc.
Anyone that can give me a clue, link to a tutorial or something to read?
As of Java 6, this is supported in the SystemTray and TrayIcon classes. SystemTray has a pretty extensive example in its Javadocs:
TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
Image image = Toolkit.getDefaultToolkit().getImage("your_image/path_here.gif");
// create a action listener to listen for default action executed on the tray icon
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// execute default action of the application
// ...
}
};
// create a popup menu
PopupMenu popup = new PopupMenu();
// create menu item for the default action
MenuItem defaultItem = new MenuItem(...);
defaultItem.addActionListener(listener);
popup.add(defaultItem);
/// ... add other items
// construct a TrayIcon
trayIcon = new TrayIcon(image, "Tray Demo", popup);
// set the TrayIcon properties
trayIcon.addActionListener(listener);
// ...
// add the tray image
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
// ...
} else {
// disable tray option in your application or
// perform other actions
...
}
// ...
// some time later
// the application state has changed - update the image
if (trayIcon != null) {
trayIcon.setImage(updatedImage);
}
// ...
You could also check out this article, or this tech tip.
It's very simple
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class SystemTrayDemo{
//start of main method
public static void main(String []args){
//checking for support
if(!SystemTray.isSupported()){
System.out.println("System tray is not supported !!! ");
return ;
}
//get the systemTray of the system
SystemTray systemTray = SystemTray.getSystemTray();
//get default toolkit
//Toolkit toolkit = Toolkit.getDefaultToolkit();
//get image
//Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
Image image = Toolkit.getDefaultToolkit().getImage("src/images/1.gif");
//popupmenu
PopupMenu trayPopupMenu = new PopupMenu();
//1t menuitem for popupmenu
MenuItem action = new MenuItem("Action");
action.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Action Clicked");
}
});
trayPopupMenu.add(action);
//2nd menuitem of popupmenu
MenuItem close = new MenuItem("Close");
close.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayPopupMenu.add(close);
//setting tray icon
TrayIcon trayIcon = new TrayIcon(image, "SystemTray Demo", trayPopupMenu);
//adjust to default size as per system recommendation
trayIcon.setImageAutoSize(true);
try{
systemTray.add(trayIcon);
}catch(AWTException awtException){
awtException.printStackTrace();
}
System.out.println("end of main");
}//end of main
}//end of class
Set appropriate path for image and then run the program. t.y. :)
This is the code you can use to access and customize the system tray:
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Performed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
// System Tray is not supported
}

Categories

Resources