2

I am able to do this in the single application, but I need to do this with two different application:

  • One to add a SystemTray icon
  • Second to remove the SystemTray icon

This is what I have tried so far, which works good in single application:

public static void main(String[] args) throws Exception {
    // Check the SystemTray support
    if (!SystemTray.isSupported()) {
        logger.info("SystemTray is not supported");
        return;
    }

    final PopupMenu popup = new PopupMenu();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream("img.png");
    Image img = ImageIO.read(inputStream);

    final TrayIcon trayIcon = new TrayIcon(img, TOOL_TIP);
    processTrayIcon = trayIcon;
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem startItem = new MenuItem("Start");

    MenuItem stopItem = new MenuItem("Stop");

    MenuItem exitItem = new MenuItem("Exit");

    // Add components to popup menu
    popup.add(startItem);
    popup.add(stopItem);
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

    trayIcon.setImageAutoSize(true);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        logger.error("TrayIcon could not be added.", e);
        return;
    }

    // Add listener to exitItem.
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            stopProcess();
            tray.remove(trayIcon);
            System.exit(0);
        }
    });

    // Add listener to startItem.
    startItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            startProcess();
        }
    });

    // Add listener to stopItem.
    stopItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            stopProcess();
        }
    });
}

/**
 * This method will start a thread that will 
 * show a popup message from the system tray when the application started successfully.
 */

private static void startProcess() {

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
        // Starts a service

        }
    });

    thread.start();
}

/**
 * This method will start a thread that will 
 * show a popup message from the system tray when the application stopped successfully.
 */

private static void stopProcess() {

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            //Stops a service           
        }
    });

    thread.start();
}

Any help is appreciated..

Shakthi
  • 175
  • 2
  • 13
  • 1
    You could [implement some messaging](http://stackoverflow.com/questions/1680898/communication-between-two-separate-java-desktop-applications) between the two applications. Your second application should then send a message to your first application to remove the system tray icon. – Balder Jan 14 '15 at 15:03

0 Answers0