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:de.tbuchloh.kiskis.gui.systray.Java6SystemTray.java

/**
 * @see de.tbuchloh.kiskis.gui.systray.ISystemTray#dispose()
 *///from w  w w.j  a  va  2 s  .c o  m
public void dispose() {
    if (_trayIcon != null) {
        SystemTray.getSystemTray().remove(_trayIcon);
        _trayIcon = null;
    }
}

From source file:misc.TrayIconDemo.java

private static void createAndShowGUI() {
    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;//from  w w  w . j  a  v  a  2 s  .c o m
    }
    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About");
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    Menu displayMenu = new Menu("Display");
    MenuItem errorItem = new MenuItem("Error");
    MenuItem warningItem = new MenuItem("Warning");
    MenuItem infoItem = new MenuItem("Info");
    MenuItem noneItem = new MenuItem("None");
    MenuItem exitItem = new MenuItem("Exit");

    //Add components to popup menu
    popup.add(aboutItem);
    popup.addSeparator();
    popup.add(cb1);
    popup.add(cb2);
    popup.addSeparator();
    popup.add(displayMenu);
    displayMenu.add(errorItem);
    displayMenu.add(warningItem);
    displayMenu.add(infoItem);
    displayMenu.add(noneItem);
    popup.add(exitItem);

    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) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
        }
    });

    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from the About menu item");
        }
    });

    cb1.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb1Id = e.getStateChange();
            if (cb1Id == ItemEvent.SELECTED) {
                trayIcon.setImageAutoSize(true);
            } else {
                trayIcon.setImageAutoSize(false);
            }
        }
    });

    cb2.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb2Id = e.getStateChange();
            if (cb2Id == ItemEvent.SELECTED) {
                trayIcon.setToolTip("Sun TrayIcon");
            } else {
                trayIcon.setToolTip(null);
            }
        }
    });

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MenuItem item = (MenuItem) e.getSource();
            //TrayIcon.MessageType type = null;
            System.out.println(item.getLabel());
            if ("Error".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.ERROR;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an error message",
                        TrayIcon.MessageType.ERROR);

            } else if ("Warning".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.WARNING;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is a warning message",
                        TrayIcon.MessageType.WARNING);

            } else if ("Info".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.INFO;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message",
                        TrayIcon.MessageType.INFO);

            } else if ("None".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.NONE;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an ordinary message",
                        TrayIcon.MessageType.NONE);
            }
        }
    };

    errorItem.addActionListener(listener);
    warningItem.addActionListener(listener);
    infoItem.addActionListener(listener);
    noneItem.addActionListener(listener);

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

From source file:de.tbuchloh.kiskis.gui.systray.Java6SystemTray.java

/**
 * @see de.tbuchloh.kiskis.gui.systray.ISystemTray#show()
 *//*from  w w  w  . j  a v a 2s  . c  o m*/
public void show() {
    if (!SystemTray.isSupported()) {
        LOG.error("System tray is not supported!");
        return;
    }

    final SystemTray tray = SystemTray.getSystemTray();
    final Image image = Toolkit.getDefaultToolkit().getImage(_main.getTrayIconURL());

    final MouseListener mouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() >= 2) {
                _main.setVisible(!_main.isVisible());
            }
        }

    };

    final PopupMenu popup = _main.getPopupMenu();

    _trayIcon = new TrayIcon(image, BuildProperties.getFullTitle(), popup);

    _trayIcon.setImageAutoSize(true);
    _trayIcon.addMouseListener(mouseListener);

    try {
        tray.add(_trayIcon);
    } catch (final AWTException e) {
        e.printStackTrace();
    }
}

From source file:org.keyboardplaying.messaging.ui.ApplicationManager.java

private void makeTrayIcon() {
    ImageLoader imgLoader = new ImageLoader();

    Image icon = imgLoader.getImage("messaging", IconSize.W_16);
    trayIcon = new TrayIcon(icon);
    int width = trayIcon.getSize().width;
    if (width > 16) {
        icon = imgLoader.getImage("messaging", IconSize.W_32);
    }//from  ww  w .  ja v  a2  s .co  m
    trayIcon.setImage(icon.getScaledInstance(width, -1, Image.SCALE_SMOOTH));

    // Add a menu
    PopupMenu popup = makeMenu();
    trayIcon.setPopupMenu(popup);

    try {
        SystemTray.getSystemTray().add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
    }
}

From source file:com.github.cmisbox.ui.UI.java

private UI() {
    this.log = LogFactory.getLog(this.getClass());
    try {//from  ww w  . j  av a 2s  .c  o m
        this.available = !GraphicsEnvironment.isHeadless();

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        if (SystemTray.isSupported()) {

            this.tray = SystemTray.getSystemTray();
            Image image = ImageIO.read(this.getClass().getResource("images/cmisbox.png"));

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Main.exit(0);
                }
            };

            this.popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem(Messages.exit);
            defaultItem.addActionListener(exitListener);
            this.popup.add(defaultItem);

            MenuItem loginItem = new MenuItem(Messages.login);
            loginItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    new LoginDialog();
                }
            });
            this.popup.add(loginItem);

            MenuItem treeItem = new MenuItem(Messages.showTree);
            treeItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    new TreeSelect();
                }
            });

            this.popup.add(treeItem);

            final TrayIcon trayIcon = new TrayIcon(image, UI.NOTIFY_TITLE, this.popup);

            trayIcon.setImageAutoSize(true);

            this.tray.add(trayIcon);

            this.notify(Messages.startupComplete);

        }

    } catch (Exception e) {
        this.log.error(e);
    }
}

From source file:org.keyboardplaying.messaging.ui.ApplicationManager.java

private void destroyTrayIcon() {
    if (trayIcon != null) {
        SystemTray.getSystemTray().remove(trayIcon);
    }
}

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");
        }/*from  w  w  w.j  av a 2s.co  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");
    }
}

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

private void initTrayIconExitHandler(Runnable exitCommand) {
    final TrayIcon trayIcon = createTrayIcon(exitCommand);
    try {//from  w ww.j a  va2 s  .c  o  m
        // 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:de.mirkosertic.desktopsearch.DesktopSearch.java

@Override
public void start(Stage aStage) throws Exception {

    // This is our base directory
    File theBaseDirectory = new File(SystemUtils.getUserHome(), "FreeSearchIndexDir");
    theBaseDirectory.mkdirs();/*w w  w . j  av a  2 s.c o m*/

    configurationManager = new ConfigurationManager(theBaseDirectory);

    Notifier theNotifier = new Notifier();

    stage = aStage;

    // Create the known preview processors
    PreviewProcessor thePreviewProcessor = new PreviewProcessor();

    try {
        // Boot the search backend and set it up for listening to configuration changes
        backend = new Backend(theNotifier, configurationManager.getConfiguration(), thePreviewProcessor);
        configurationManager.addChangeListener(backend);

        // Boot embedded JSP container
        embeddedWebServer = new FrontendEmbeddedWebServer(aStage, backend, thePreviewProcessor,
                configurationManager);

        embeddedWebServer.start();
    } catch (BindException | LockReleaseFailedException | LockObtainFailedException e) {
        // In this case, there is already an instance of DesktopSearch running
        // Inform the instance to bring it to front end terminate the current process.
        URL theURL = new URL(FrontendEmbeddedWebServer.getBringToFrontUrl());
        // Retrieve the content, but it can be safely ignored
        // There must only be the get request
        Object theContent = theURL.getContent();

        // Terminate the JVM. The window of the running instance is visible now.
        System.exit(0);
    }

    aStage.setTitle("Free Desktop Search");
    aStage.setWidth(800);
    aStage.setHeight(600);
    aStage.initStyle(StageStyle.TRANSPARENT);

    FXMLLoader theLoader = new FXMLLoader(getClass().getResource("/scenes/mainscreen.fxml"));
    AnchorPane theMainScene = theLoader.load();

    final DesktopSearchController theController = theLoader.getController();
    theController.configure(this, backend, FrontendEmbeddedWebServer.getSearchUrl(), stage.getOwner());

    Undecorator theUndecorator = new Undecorator(stage, theMainScene);
    theUndecorator.getStylesheets().add("/skin/undecorator.css");

    Scene theScene = new Scene(theUndecorator);

    // Hacky, but works...
    theUndecorator.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");

    theScene.setFill(Color.TRANSPARENT);
    aStage.setScene(theScene);

    aStage.getIcons().add(new Image(getClass().getResourceAsStream("/fds.png")));

    if (SystemTray.isSupported()) {
        Platform.setImplicitExit(false);
        SystemTray theTray = SystemTray.getSystemTray();

        // We need to reformat the icon according to the current tray icon dimensions
        // this depends on the underlying OS
        java.awt.Image theTrayIconImage = Toolkit.getDefaultToolkit()
                .getImage(getClass().getResource("/fds_small.png"));
        int trayIconWidth = new TrayIcon(theTrayIconImage).getSize().width;
        TrayIcon theTrayIcon = new TrayIcon(
                theTrayIconImage.getScaledInstance(trayIconWidth, -1, java.awt.Image.SCALE_SMOOTH),
                "Free Desktop Search");
        theTrayIcon.setImageAutoSize(true);
        theTrayIcon.setToolTip("FXDesktopSearch");
        theTrayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    Platform.runLater(() -> {
                        if (stage.isIconified()) {
                            stage.setIconified(false);
                        }
                        stage.show();
                        stage.toFront();
                    });
                }
            }
        });
        theTray.add(theTrayIcon);

        aStage.setOnCloseRequest(aEvent -> stage.hide());
    } else {

        aStage.setOnCloseRequest(aEvent -> shutdown());
    }

    aStage.setMaximized(true);
    aStage.show();
}

From source file:org.exist.launcher.Launcher.java

public Launcher(final String[] args) {
    if (SystemTray.isSupported()) {
        tray = SystemTray.getSystemTray();
    }//from  w  w  w. j ava 2 s  .c o  m

    captureConsole();

    this.jettyConfig = getJettyConfig();
    final Optional<Path> eXistHome = ConfigurationHelper.getExistHome();
    final Path wrapperConfig;
    if (eXistHome.isPresent()) {
        wrapperConfig = eXistHome.get().resolve("tools/yajsw/conf/wrapper.conf");
    } else {
        wrapperConfig = Paths.get("tools/yajsw/conf/wrapper.conf");
    }

    wrapperProperties = new Properties();
    wrapperProperties.setProperty("wrapper.working.dir", eXistHome.orElse(Paths.get(".")).toString());
    wrapperProperties.setProperty("wrapper.config", wrapperConfig.toString());

    System.setProperty("wrapper.config", wrapperConfig.toString());

    installedAsService();

    boolean initSystemTray = true;
    if (isSystemTraySupported()) {
        initSystemTray = initSystemTray();
    }

    configDialog = new ConfigurationDialog(this::shutdown);

    splash = new SplashScreen(this);
    splash.addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent windowEvent) {
            setServiceState();
            if (runningAsService.isPresent()) {
                splash.setStatus("eXist-db is already installed as service! Attaching to it ...");
                final Timer timer = new Timer(3000, (event) -> splash.setVisible(false));
                timer.setRepeats(false);
                timer.start();
            } else {
                if (ConfigurationUtility.isFirstStart()) {
                    splash.setVisible(false);
                    configDialog.open(true);
                    configDialog.requestFocus();
                } else {
                    startJetty();
                }
            }
        }
    });

    final boolean systemTrayReady = tray != null && initSystemTray && tray.getTrayIcons().length > 0;

    SwingUtilities.invokeLater(() -> utilityPanel = new UtilityPanel(Launcher.this, systemTrayReady));
}