Question

I am writing a system tray app in Java for OS X that gets the users Computer Name, MAC address and IP address. I got the information and MenuItems loaded fine. My issue is updating the MenuItems when the user's information changes. I added a mouse listener that would rebuild the MenuItems when the user clicks on the tray icon. I added a popup.removeall() and then redo my MenuItems, but it doesn't work. What is the correct way to update my MenuItems?

public void mouseClicked(MouseEvent e) 
                {

                    try
                    {
                    popup.removeAll();



                    PopupMenu popup = new PopupMenu();
                    MenuItem defaultItem = new MenuItem("Exit");
                    defaultItem.addActionListener(exitListener);
                    MenuItem userIP1 = new MenuItem(getIP());
                    MenuItem userMAC1 = new MenuItem(getMac());
                    MenuItem computerName1 = new MenuItem(getComputerName());


                    popup.add(computerName1);
                    popup.add(userMAC1);
                    popup.add(userIP1);
                    popup.add(defaultItem);
                    try
                    {
                    tray.add(trayIcon);
                    }
                    catch(Exception e2)
                    {
                        //Empty
                    }

                    }


                    catch(UnknownHostException e1)
                    {
                        //Empty

                    }
                }
Était-ce utile?

La solution

I think you just need to remove this line:

PopupMenu popup = new PopupMenu();

Which will cause all the following lines to access the local variable popup rather than the field you're expecting to update.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top