Example usage for java.awt TrayIcon addMouseListener

List of usage examples for java.awt TrayIcon addMouseListener

Introduction

In this page you can find the example usage for java.awt TrayIcon addMouseListener.

Prototype

public synchronized void addMouseListener(MouseListener listener) 

Source Link

Document

Adds the specified mouse listener to receive mouse events from this TrayIcon .

Usage

From source file:org.cryptomator.ui.ExitUtil.java

private void initTrayIconExitHandler(Runnable exitCommand) {
    final TrayIcon trayIcon = createTrayIcon(exitCommand);
    try {/*from  www  . j  a v  a  2s  .c om*/
        // double clicking tray icon should open Cryptomator
        if (SystemUtils.IS_OS_WINDOWS) {
            trayIcon.addMouseListener(new TrayIconMouseListener());
        }

        SystemTray.getSystemTray().add(trayIcon);
        mainWindow.setOnCloseRequest((e) -> {
            if (Platform.isImplicitExit()) {
                exitCommand.run();
            } else {
                macFunctions.map(MacFunctions::uiState)
                        .ifPresent(JniException.ignore(MacApplicationUiState::transformToAgentApplication));
                mainWindow.close();
                this.showTrayNotification(trayIcon);
            }
        });
    } catch (SecurityException | AWTException ex) {
        // not working? then just go ahead and close the app
        mainWindow.setOnCloseRequest((ev) -> {
            exitCommand.run();
        });
    }
}

From source file:org.kootox.episodesmanager.ui.systray.EpisodesTrayIcon.java

public void create() {

    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        if (log.isInfoEnabled()) {
            log.info("SystemTray is not supported");
        }/* w ww.  jav  a  2  s.  c  o  m*/
        return;
    }

    if (loaded) {
        return;
    }

    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("systray.png", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem display = new MenuItem("Display");
    MenuItem exit = new MenuItem("Exit");

    //Add components to popup menu
    popup.add(display);
    popup.addSeparator();
    popup.add(exit);

    trayIcon.setPopupMenu(popup);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        if (log.isDebugEnabled()) {
            log.debug("TrayIcon could not be added.");
        }
        return;
    }

    trayIcon.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
                showHide();
            }
        }

        @Override
        public void mousePressed(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseReleased(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseEntered(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseExited(MouseEvent mouseEvent) {
            //Do nothing
        }
    });

    display.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            showHide();
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            EpisodesManagerMainUI mainUI = EpisodesManagerContext.MAIN_UI_ENTRY_DEF.getContextValue(context);
            mainUI.close();
        }
    });

    loaded = true;
    if (log.isDebugEnabled()) {
        log.debug("Systray loaded");
    }
}