Results 1 to 8 of 8
  1. #1
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Icon not showing up in system tray

    I used the code from the following site:
    How to Use the System Tray (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    in order to have an icon for my app, but the icon only shows up when I'm running the application inside Netbeans.
    When I run the .jar outside of Netbeans, the icon never shows up.
    How can I fix this?
    Thanks in advance!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Icon not showing up in system tray

    show us the part of your code where the icon is created. you have to use a url object like the example: How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Icon not showing up in system tray

    This is where I create the icon.

    Java Code:
    private static void createAndShowGUI() {
            //Check the SystemTray support
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return;
            }
            final PopupMenu popup = new PopupMenu();
            File f = new File("images\\Battery2.gif");
            
            String ruta = f.getPath();
            System.out.println("ruta: " +ruta );
            final TrayIcon trayIcon =  new TrayIcon(createImage(ruta, "tray icon"));
            final SystemTray tray = SystemTray.getSystemTray();
             
            // Create a popup menu components
            MenuItem open = new MenuItem("Open");
            MenuItem aboutItem = new MenuItem("About");
            CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");        
            MenuItem exitItem = new MenuItem("Exit");
    //        CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    //        Menu displayMenu = new Menu("Display");        
    //        MenuItem errorItem = new MenuItem("Error");
    //        MenuItem warningItem = new MenuItem("Warning");
    //        MenuItem infoItem = new MenuItem("Info");
    //        MenuItem noneItem = new MenuItem("None");
            
             
            //Add components to popup menu
            popup.add(open);
            popup.add(aboutItem);
    //        popup.addSeparator();
    //        popup.add(cb1);
    //        popup.add(cb2);
    //        popup.addSeparator();
    //        popup.add(displayMenu);        
    //        displayMenu.add(errorItem);
    //        displayMenu.add(warningItem);
    //        displayMenu.add(infoItem);
    //        displayMenu.add(noneItem);
            popup.add(exitItem);
             
            trayIcon.setPopupMenu(popup);
             
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.out.println("TrayIcon could not be added.");
                return;
            }
             
            trayIcon.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
    //                JOptionPane.showMessageDialog(null,
    //                        "This dialog box is run from System Tray");
                    Principal p;
                    try {
                        p = new Principal();
                        p.setVisible(true);
                    } catch (Exception ex) {
                        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }
            });
             
            aboutItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon icon = new ImageIcon(Principal.class.getResource("images/Battery.gif"));
                    JOptionPane.showMessageDialog(null,"This software was created by ProTrchHN","About",JOptionPane.INFORMATION_MESSAGE,icon);
                }
            });
             
            open.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                Principal p;
                    try {
                        p = new Principal();
                        p.setVisible(true);
                    } catch (Exception ex) {
                        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
            });
            cb1.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    int cb1Id = e.getStateChange();
                    if (cb1Id == ItemEvent.SELECTED){
                        trayIcon.setImageAutoSize(true);
                    } else {
                        trayIcon.setImageAutoSize(true);
                    }
                }
            });
             
    //        cb2.addItemListener(new ItemListener() {
    //            public void itemStateChanged(ItemEvent e) {
    //                int cb2Id = e.getStateChange();
    //                if (cb2Id == ItemEvent.SELECTED){
    //                    trayIcon.setToolTip("Sun TrayIcon");
    //                } else {
    //                    trayIcon.setToolTip(null);
    //                }
    //            }
    //        });                
             
            ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    MenuItem item = (MenuItem)e.getSource();
                    //TrayIcon.MessageType type = null;
                    System.out.println(item.getLabel());
                    if ("Error".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.ERROR;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an error message", TrayIcon.MessageType.ERROR);
                         
                    } else if ("Warning".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.WARNING;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is a warning message", TrayIcon.MessageType.WARNING);
                         
                    } else if ("Info".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.INFO;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an info message", TrayIcon.MessageType.INFO);
                         
                    } else if ("None".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.NONE;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an ordinary message", TrayIcon.MessageType.NONE);
                    }
                }
            };
             
    //        errorItem.addActionListener(listener);
    //        warningItem.addActionListener(listener);
    //        infoItem.addActionListener(listener);
    //        noneItem.addActionListener(listener);
             
            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    System.exit(0);
                }
            });
        }
         
        //Obtain the image URL
        protected static Image createImage(String path, String description) {
            URL imageURL = Principal.class.getResource(path);
             
            if (imageURL == null) {
                return null;
                
            } else {
            
                return (new ImageIcon(path, description)).getImage();
           }
        }

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Icon not showing up in system tray

    Java Code:
    File f = new File("images\\Battery2.gif");
    Where do you expect that file to be loaded from? This says "load the image from an images subdirectory inside the directory where java is run".

    You will likely want to load the image as a resource from the project classpath.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Icon not showing up in system tray

    How do I do that, because I am using a library that in order to beused, I had to change the directory from which the project runs from. This is where I am loading the image from.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Icon not showing up in system tray

    There is no more than one way to do it, you simply add the image to the application's classpath and you load it as a resource. That way it works wherever, whenever.

    Retrieving Resources (The Java™ Tutorials > Deployment > Java Web Start)

    http://docs.oracle.com/javase/tutori...ents/icon.html

    http://www.javaworld.com/javaqa/2003...-property.html
    Last edited by gimbal2; 09-05-2013 at 04:56 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Icon not showing up in system tray

    I created a class in order to load the image in a file:
    Java Code:
    import java.io.File;
    import javax.swing.*;
    
    public class Imagen {
    
        public File setImage() {
            File img;
            img = new File(getClass().getResource("/Resources/Battery2.gif").toString());
            return img;
        }
    }
    Then I the .getpath(), in order to get the path:
    Java Code:
    trayIcon = new TrayIcon(createImage(img.setImage().getPath(), "tray icon"));
    But I'm getting: "Creting TrayIcon with null image".
    How can I fix this, I already have the the image in a package called "Resources".
    Thanks in advance!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Icon not showing up in system tray

    No, you want to simply getResource, which gives you a URL, which you pass to the ImageIcon constructor.
    Though I've normally seen it done using ImageIO and the read() method, and getResourceAsStream(). Which gives you an Image straight out, without faffing with an ImageIcon.

    As soon as you go via File then you are attempting to access the file system...and in the caseof your jar the image is not on the file system, it's in the jar file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. add application to system tray monitor
    By Jennifer Davis in forum NetBeans
    Replies: 1
    Last Post: 09-25-2010, 02:31 PM
  2. System Tray
    By 67726e in forum AWT / Swing
    Replies: 8
    Last Post: 08-12-2010, 04:21 PM
  3. Replies: 0
    Last Post: 07-25-2008, 02:32 PM
  4. System Tray
    By JavaLerner in forum New To Java
    Replies: 2
    Last Post: 07-13-2008, 08:25 AM
  5. [SOLVED] Baloon message on a tray icon
    By Eranga in forum AWT / Swing
    Replies: 5
    Last Post: 04-10-2008, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •