Search code examples
javaawtsystem-traytrayicon

Unable to make the MenuItem in TrayIcon clickable


I made a simple java program to display a system tray with many menu items that appears on clicking it. All these menu items are meant to be clickable.

I included the code of tray icon functionalities inside an ActionListener.

As of my I have included many options but the ActionListener of none is working.

The program throws no error but I'm unable to include further functions for the buttons.

This is my code:


    
    public static void showTray()
    {
        if (systemTray==null)
        {           
            System.exit(0); 
        }
        else
        {
            System.out.println("SystemTray works"); 
        }
        //String[] args= {"",""};
          
        // ImageIcon icon = new ImageIcon("/home/ioss/Downloads/hexagon-clipart.jpeg");
        systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
        systemTray.setStatus("Running"); 
        

        // MenuItem user_email=new MenuItem(email);
        MenuItem chgImg=new MenuItem("Change Icon");    
        MenuItem link =new MenuItem ("My Desklog"); 
            MenuItem version=new MenuItem("Version-1.0.4");
           
            systemTray.getMenu().add(link).setShortcut('q');
            systemTray.getMenu().add(version).setShortcut('q');
            
          
           
//          systemTray.getMenu().add(of).setShortcut('q');
//          systemTray.getMenu().add(on).setShortcut('q');
//          systemTray.getMenu().add(logout1).setShortcut('q');
            systemTray.getMenu().add(chgImg).setShortcut('q');
        
        PopupMenu pm=new PopupMenu();   
        
        op=new MenuItem("About"); 
          systemTray.getMenu().add(op).setShortcut('q');
        
        on =new MenuItem("Go Online");
         systemTray.getMenu().add(on).setShortcut('q');
         on.setEnabled(false);
        
ActionListener online = new ActionListener() {

            
            @Override
            public void actionPerformed(ActionEvent arg0) { 
                System.out.println("Inside Online Listener"); 
                systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
                    
                    System.out.println("On enabled");
                    
                of.setEnabled(true);
                System.out.println("online..");
                systemTray.setStatus("online");                 
            }
            
        };
        
        of =new MenuItem("Go Offline");
        
        
        ActionListener offline = new ActionListener() {

            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                
                systemTray.setImage("/home/ioss/Downloads/prohibition-symbol.jpeg"); 
                on.setEnabled(true);
                of.setEnabled(false);
                System.out.println("offline..");
                systemTray.setStatus("offline"); 
                
            }
            
        };
          systemTray.getMenu().add(of).setShortcut('q');
        
//  PopupMenu pm=new PopupMenu();   
//      of =new MenuItem("Go Offline");
//      op=new MenuItem("About"); 
        
//  pm.add(of);
    
    
//  pm.add(on);
    
//      on =new MenuItem("Go Online",online);
        
        //My Desklog
    
        
        ActionListener myDesklog =new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        
                        
                    
                        try {
                            System.out.println("Desklog Listener"); 
//                          Desktop.getDesktop().browse(new URI("https://app.desklog.io/user/home#"));
                            
                            String url_open ="http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860";
//                          java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open));
                            
                            java.awt.Desktop.getDesktop().browse(new URI("https://app.desklog.io/user/home#"));
                            
                            
                        } catch (IOException e) {
                            
                            e.printStackTrace();
                        } catch (URISyntaxException e) {
                            
                            e.printStackTrace();
                        }
                        
                    }
            
                };
                //LOGOUT 
        MenuItem logout =new MenuItem("Logout");
        
          ActionListener logout1 =new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    System.exit(0);
                    
                }
        
            }; 
        
                
        
        
        ///////////////Change Icon
        
ActionListener ChangeIcon = new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent arg0) {
                
                System.out.println("Inside ActionLisener"); 
                 JButton button1 = new JButton("One");
                 JButton button2 = new JButton("Two");
                  
                 
                  
                  Icon icon = new ImageIcon("/home/ioss/Downloads/hexagon-clipart.jpeg");
                  JButton button3 = new JButton(icon);
                  Box box = Box.createVerticalBox();
                  box.add(button1);
                  box.add(button2);
                  box.add(button3);
                 
                
                  JFrame frame = new JFrame();
                  frame.add(box);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationByPlatform(true);
                  frame.setSize(500, 300);
                  frame.setVisible(true);
                
            }
            
        };
    
    }
    }




Solution

  • Try adding an implements method inside the actonListener, it necessarily not need a body. Something like this:

    public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    
                }