jvilla1983
asked on
having troubles getting icon to show up in the system tray
OK.. My code here that I derived from another website isnt showing the icon that I chose to use for my system tray. Is there any way that someone could help me out with getting it to show up there?
package main;
import java.awt.*;
import java.awt.event.*;
public class NewControllerClass {
public NewControllerClass() {
final TrayIcon trayIcon;
if ( SystemTray.isSupported() ) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("bomb.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("Exit.. ");
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 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 {
// do nothing..
}
}
ASKER
Yeah.. that didn't help too much. I've looked at many Java Documents on this feature and none have seemed to help.. Here is my code..
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import display.*;
import javax.swing.plaf.metal.*;
/**
*
* @author jvilla
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
SystemTray tray = SystemTray.getSystemTray();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("bomb.gif");
PopupMenu menu = new PopupMenu();
MenuItem messageItem = new MenuItem("About this program..");
messageItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Created by Joseph Villa for USGS");
}
});
menu.add(messageItem);
MenuItem showDisplayProjects = new MenuItem("Display Projects Statistics");
showDisplayProjects.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GraphicController g = new GraphicController("IGSBCCCMGS0002.Projects");
}
});
menu.add(showDisplayProjects);
MenuItem showDisplayUsers = new MenuItem("Display Users Statistics");
showDisplayProjects.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GraphicController g = new GraphicController("ISGBCCCMGS0002.Users");
}
});
menu.add(showDisplayUsers);
MenuItem SaveStats = new MenuItem("Save Statistics");
SaveStats.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menu.add(SaveStats);
MenuItem closeItem = new MenuItem("Close");
closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(closeItem);
TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
icon.setImageAutoSize(true);
try {
tray.add(icon);
} catch ( AWTException x ) {
x.printStackTrace();
}
}
}
ASKER
Forget the first posting of this.. I changed my code around quite a bit since then. ;-)
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
See, I had used much of the code from the second link that you gave me and it was a no-go. If you notice, my first posting almost mirrors what is being done in the second link.
I'm running the latest version of Java 6 available at Java.sun.com.
I'm running the latest version of Java 6 available at Java.sun.com.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
http://blogs.sun.com/CoreJavaTechTips/entry/getting_to_know_system_tray