Example usage for java.awt SystemTray getSystemTray

List of usage examples for java.awt SystemTray getSystemTray

Introduction

In this page you can find the example usage for java.awt SystemTray getSystemTray.

Prototype

public static SystemTray getSystemTray() 

Source Link

Document

Gets the SystemTray instance that represents the desktop's tray area.

Usage

From source file:ninja.eivind.hotsreplayuploader.Client.java

private void addToTray(final Stage primaryStage) {
    try {// ww  w.  j ava2 s. c o  m
        TrayIcon trayIcon = platformService.getTrayIcon(primaryStage);

        // update tooltip when the statusbinder changes status
        statusBinder.message().addListener((observable, oldValue, newValue) -> {
            if (newValue != null && !newValue.isEmpty()) {
                trayIcon.setToolTip("Status: " + newValue);
            }
        });

        final SystemTray systemTray = SystemTray.getSystemTray();
        systemTray.add(trayIcon);

    } catch (PlatformNotSupportedException | AWTException e) {
        LOG.warn("Could not instantiate tray icon. Reverting to default behaviour");
        primaryStage.setOnCloseRequest(event -> Platform.exit());
    }
}

From source file:iqq.app.ui.manager.MainManager.java

public void enableTray() {
    if (SystemTray.isSupported() && tray == null) {
        menu = new PopupMenu();
        MenuItem restore = new MenuItem("  ?  ");
        restore.addActionListener(new ActionListener() {
            @Override/*from   w  w  w.  jav  a2s.c o  m*/
            public void actionPerformed(ActionEvent e) {
                show();
            }
        });
        MenuItem exit = new MenuItem("  ?  ");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(restore);
        menu.addSeparator();
        menu.add(exit);

        if (SystemUtils.isMac()) {
            defaultImage = skinService.getIconByKey("window/titleWIconBlack").getImage();
        } else {
            defaultImage = mainFrame.getIconImage();
        }
        blankImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        tray = SystemTray.getSystemTray();
        icon = new TrayIcon(defaultImage, "IQQ");
        icon.setImageAutoSize(true);
        if (!SystemUtils.isMac()) {
            icon.setPopupMenu(menu);
        }
        try {
            tray.add(icon);
            icon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    logger.debug("MouseEvent " + e.getButton() + " " + e.getClickCount());
                    //??
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        if (flashOwner != null) {
                            Map<String, Object> data = (Map<String, Object>) flashOwner.getAttachment();
                            if (data != null && data.containsKey("action")
                                    && data.get("action").equals("ADD_BUDDY_REQUEST")) {
                                IMContext.getBean(FrameManager.class).showGetFriendRequest((IMBuddy) flashOwner,
                                        data.get("buddy_request_id").toString());
                                eventService.broadcast(new UIEvent(UIEventType.FLASH_USER_STOP, flashOwner));
                                return;
                            } else {
                                // ?
                                chatManager.addChat(flashOwner);
                            }
                        } else {
                            show();
                        }
                    }
                }
            });
        } catch (AWTException e) {
            logger.error("SystemTray add icon.", e);
        }
    }

}

From source file:zxmax.tools.timerreview.Main.java

private TrayIcon getTrayIcon() throws AWTException {
    final TrayIcon trayIcon = new TrayIcon(
            createImage("images/bulb.gif", I18N.getLabel(getClass(), "tray.icon.description")));

    final SystemTray tray = SystemTray.getSystemTray();
    tray.add(trayIcon);//from w  ww.  ja  v  a2 s.  com

    final PopupMenu popup = new PopupMenu();
    trayIcon.setPopupMenu(popup);

    /*
     * Serve per aprire il popup sulla tray icon con il tasto sx del mouse.
     * Il problema  chiuderla/nasconderla ...
     */
    // trayIcon.addMouseListener(new TrayIconMouseAdapter(popup));

    final MenuItem exitItem = new MenuItem(I18N.getLabel(getClass(), "exit"));
    final MenuItem newTimerItem = new MenuItem(I18N.getLabel(getClass(), "new.timer"));
    newTimerItem.setEnabled(false);
    final MenuItem infoItem = new MenuItem(I18N.getLabel(getClass(), "info"));
    popup.add(exitItem);
    popup.add(newTimerItem);
    popup.add(infoItem);

    exitItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tray.remove(trayIcon);
            System.exit(0);
        }
    });

    newTimerItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            StartTimerWindow timer = new StartTimerWindow();
            timer.setVisible(true);
        }
    });

    infoItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            InfoWindow infoWindow = new InfoWindow();
            infoWindow.setVisible(true);
            infoWindow.createBufferStrategy(1);
        }
    });
    return trayIcon;
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage resizeImageToTray(BufferedImage image) {
    SystemTray tray = SystemTray.getSystemTray();
    Dimension dimension = tray.getTrayIconSize();
    return resizeImage(image, dimension.width, dimension.height);
}

From source file:org.duracloud.syncui.SyncUIDriver.java

private static void createSysTray(final String url, final Server srv) {
    final TrayIcon trayIcon;
    try {//from  ww  w  .  j  a  va2s  .  c o m

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            InputStream is = org.duracloud.syncui.SyncUIDriver.class.getClassLoader()
                    .getResourceAsStream("tray.png");

            Image image = ImageIO.read(is);
            MouseListener mouseListener = new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    log.debug("Tray Icon - Mouse clicked!");
                }

                public void mouseEntered(MouseEvent e) {
                    log.debug("Tray Icon - Mouse entered!");
                }

                public void mouseExited(MouseEvent e) {
                    log.debug("Tray Icon - Mouse exited!");
                }

                public void mousePressed(MouseEvent e) {
                    log.debug("Tray Icon - Mouse pressed!");
                }

                public void mouseReleased(MouseEvent e) {
                    log.debug("Tray Icon - Mouse released!");
                }
            };

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    log.info("Exiting...");
                    try {
                        srv.stop();
                        while (!srv.isStopped()) {
                            WaitUtil.wait(1);
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }

                    System.exit(0);
                }
            };

            PopupMenu popup = new PopupMenu();
            MenuItem view = new MenuItem("View Status");
            view.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    launchBrowser(url);
                }
            });
            popup.add(view);

            MenuItem exit = new MenuItem("Exit");
            exit.addActionListener(exitListener);
            popup.add(exit);

            trayIcon = new TrayIcon(image, "DuraCloud Sync Tool", popup);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!",
                            TrayIcon.MessageType.INFO);
                }
            };

            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                log.error("TrayIcon could not be added.");
            }
        } else {
            log.warn("System Tray is not supported.");
        }

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

}

From source file:application.Main.java

public void createTrayIcon(final Stage stage) {
    // if the operating system
    // supports the system tray
    if (SystemTray.isSupported()) {
        // get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();
        // load an image
        java.awt.Image image = null;
        try {/*w w  w .  j  a  va  2  s  .  co  m*/
            //                File file = new File(iconLocation);
            //                image = ImageIO.read(file);
            URL urlIcon = Main.class.getResource(iconLocation);
            image = ImageIO.read(urlIcon);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                hide(stage);
            }
        });

        // create an action listener to listen for default action executed on the tray icon
        final ActionListener closeListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.close();
                        controller.terminate();
                        //                           // fileWatcher.setTerminateWatching(Boolean.TRUE);
                        System.out.println(applicationTitle + " terminated!");
                        Platform.exit();
                        System.exit(0);
                    }
                });
            }
        };

        ActionListener showListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.show();
                    }
                });
            }
        };

        // create a pop-up menu
        PopupMenu popupMenu = new PopupMenu();

        MenuItem nameItem = new MenuItem(applicationTitle);
        nameItem.addActionListener(showListener);
        popupMenu.add(nameItem);

        popupMenu.addSeparator();

        MenuItem showItem = new MenuItem("Show");
        showItem.addActionListener(showListener);
        popupMenu.add(showItem);

        MenuItem closeItem = new MenuItem("Close");
        closeItem.addActionListener(closeListener);
        popupMenu.add(closeItem);

        /// ... add other menu items

        // construct a TrayIcon, scaling the image to 16x16 (the default dimensions of a tray icon)
        trayIcon = new TrayIcon(image.getScaledInstance(24, 24, Image.SCALE_DEFAULT), applicationTitle,
                popupMenu);
        // set the TrayIcon properties
        trayIcon.addActionListener(showListener);

        // add the tray image
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
    }
}

From source file:com.devbury.desktoplib.systemtray.SystemTrayApplication.java

protected SystemTray newSystemTray() {
    return SystemTray.getSystemTray();
}

From source file:net.mybox.mybox.ClientGUI.java

private void placeTrayIconAWT() {

    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/* w  w  w.j  a v  a2 s . co m*/
    }
    final PopupMenu popup = new PopupMenu();

    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About Mybox");
    MenuItem opendirItem = new MenuItem("Open Directory");
    MenuItem prefsItem = new MenuItem("Preferences");
    MenuItem exitItem = new MenuItem("Quit Mybox");

    //Add components to popup menu

    popup.add(opendirItem);
    popup.add(pauseItem);
    popup.add(syncnowItem);

    popup.addSeparator();
    popup.add(prefsItem);
    popup.add(aboutItem);
    popup.add(connectionItem);
    popup.add(exitItem);

    trayIcon.setImageAutoSize(true);
    trayIcon.setToolTip("Mybox");

    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) {
            launchFileBrowser();
        }
    });

    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //JOptionPane.showMessageDialog(null, "Mybox!");

            Dialog dialog = new Dialog((ClientGUI) client.clientGui, "Mybox...");
            dialog.setVisible(true);

            //MessageDialog dialog = new InfoMessageDialog(null, "Mybox", "... is awesome!");//ErrorMessageDialog(null, "Error", message);
            //dialog.setTitle("About Mybox");
            //dialog.run();
            //dialog.hide();
        }
    });

    prefsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //window.showAll();
            ((ClientGUI) client.clientGui).setVisible(true);
        }
    });

    syncnowItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            client.FullSync();
        }
    });

    opendirItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            launchFileBrowser();
        }
    });

    pauseItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (pauseItem.getLabel().equals("Pause Syncing")) {
                client.pause();
                pauseItem.setLabel("Unpause Syncing");
            } else if (pauseItem.getLabel().equals("Unpause Syncing")) {
                client.unpause();
                pauseItem.setLabel("Pause Syncing");
            }
        }
    });

    connectionItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (connectionItem.getLabel().equals("Connect")) {
                client.start();
            } else if (connectionItem.getLabel().equals("Disconnect")) {
                client.stop();
            }
        }
    });

    exitItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            tray.remove(trayIcon);
            System.exit(0);
        }
    });
}

From source file:com.ethercamp.harmony.desktop.HarmonyDesktop.java

private static void setTrayMenu(TrayIcon trayIcon, MenuItem... items) {
    if (!SystemTray.isSupported()) {
        log.error("System tray is not supported");
        return;// w  w  w .  j  av a  2  s .  c  o  m
    }
    final SystemTray systemTray = SystemTray.getSystemTray();

    final PopupMenu popupMenu = new PopupMenu();
    stream(items).forEach(i -> popupMenu.add(i));
    trayIcon.setPopupMenu(popupMenu);

    try {
        stream(systemTray.getTrayIcons()).forEach(t -> systemTray.remove(t));
        systemTray.add(trayIcon);
    } catch (AWTException e) {
        log.error("Problem set tray", e);
    }
}

From source file:com.raddle.tools.ClipboardTransferMain.java

public ClipboardTransferMain() {
    super();/*from  www.  java 2  s.  c  o m*/
    initGUI();
    //// ??
    Properties p = new Properties();
    File pf = new File(System.getProperty("user.home") + "/clip-trans/conf.properties");
    if (pf.exists()) {
        try {
            p.load(new FileInputStream(pf));
            serverAddrTxt.setText(StringUtils.defaultString(p.getProperty("server.addr")));
            portTxt.setText(StringUtils.defaultString(p.getProperty("local.port")));
            modifyClipChk.setSelected("true".equals(p.getProperty("allow.modify.local.clip")));
            autoChk.setSelected("true".equals(p.getProperty("auto.modify.remote.clip")));
        } catch (Exception e) {
            updateMessage(e.getMessage());
        }
    }
    m.addListener(new ClipboardListener() {

        @Override
        public void contentChanged(Clipboard clipboard) {
            setRemoteClipboard(false);
        }
    });
    m.setEnabled(autoChk.isSelected());
    //
    try {
        pasteImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/clipboard_paste.png"));
        grayImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/clipboard_gray.png"));
        sendImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/mail-send.png"));
        BufferedImage taskImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/taskbar.png"));
        setIconImage(taskImage);
        SystemTray systemTray = SystemTray.getSystemTray();
        trayIcon = new TrayIcon(grayImage, "??");
        systemTray.add(trayIcon);
        ////
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {// ???
                    if (ClipboardTransferMain.this.isVisible()) {
                        ClipboardTransferMain.this.setState(ICONIFIED);
                    } else {
                        ClipboardTransferMain.this.setVisible(true);
                        ClipboardTransferMain.this.setState(NORMAL);
                    }
                }
            }
        });
        ////// event 
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                ClipboardTransferMain.this.setVisible(false);
                super.windowIconified(e);
            }

            @Override
            public void windowClosing(WindowEvent e) {
                File pf = new File(System.getProperty("user.home") + "/clip-trans/conf.properties");
                pf.getParentFile().mkdirs();
                try {
                    Properties op = new Properties();
                    op.setProperty("server.addr", serverAddrTxt.getText());
                    op.setProperty("local.port", portTxt.getText());
                    op.setProperty("allow.modify.local.clip", modifyClipChk.isSelected() + "");
                    op.setProperty("auto.modify.remote.clip", autoChk.isSelected() + "");
                    FileOutputStream os = new FileOutputStream(pf);
                    op.store(os, "clip-trans");
                    os.flush();
                    os.close();
                } catch (Exception e1) {
                }
                shutdown();
                super.windowClosing(e);
            }
        });
    } catch (Exception e) {
        updateMessage(e.getMessage());
    }
    Thread thread = new Thread() {

        @Override
        public void run() {
            while (true) {
                try {
                    String poll = iconQueue.take();
                    if ("send".equals(poll)) {
                        trayIcon.setImage(grayImage);
                    } else if ("paste".equals(poll)) {
                        Thread.sleep(20);
                        trayIcon.setImage(grayImage);
                    }
                } catch (InterruptedException e1) {
                    return;
                }
            }
        }
    };
    thread.setDaemon(true);
    thread.start();
}