Java Code Examples for java.awt.SystemTray#getSystemTray()

The following examples show how to use java.awt.SystemTray#getSystemTray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownApplication.java    From proxyee-down with Apache License 2.0 6 votes vote down vote up
private void initTray() throws AWTException {
  if (SystemTray.isSupported()) {
    // 获得系统托盘对象
    SystemTray systemTray = SystemTray.getSystemTray();
    // 获取图片所在的URL
    URL url = Thread.currentThread().getContextClassLoader().getResource(ICON_PATH);
    // 为系统托盘加托盘图标
    Image trayImage = Toolkit.getDefaultToolkit().getImage(url);
    Dimension trayIconSize = systemTray.getTrayIconSize();
    trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);
    trayIcon = new TrayIcon(trayImage, "Proxyee Down");
    systemTray.add(trayIcon);
    loadPopupMenu();
    //双击事件监听
    trayIcon.addActionListener(event -> Platform.runLater(() -> loadUri(null, true)));
  }
}
 
Example 2
Source File: TrayManager.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
void setTray() {
    if (!Config.getInstance().getBoolean(Config.MAIN_TRAY)) {
        this.removeTray();
        return;
    }

    if (!SystemTray.isSupported()) {
        LOGGER.info("tray icon not supported");
        return;
    }

    if (mTrayIcon == null)
        mTrayIcon = this.createTrayIcon();

    SystemTray tray = SystemTray.getSystemTray();
    if (tray.getTrayIcons().length > 0)
        return;

    try {
        tray.add(mTrayIcon);
    } catch (AWTException ex) {
        LOGGER.log(Level.WARNING, "can't add tray icon", ex);
    }
}
 
Example 3
Source File: OnlyTrayIconDemo.java    From oim-fx with MIT License 5 votes vote down vote up
private void enableTray(final Stage stage) {
	try {
		ContextMenu menu = new ContextMenu();

		MenuItem updateMenuItem = new MenuItem();
		MenuItem showMenuItem = new MenuItem();
		showMenuItem.setText("查看群信息");
		updateMenuItem.setText("修改群信息");
		menu.getItems().add(showMenuItem);
		menu.getItems().add(updateMenuItem);
		menu.getItems().add(new MenuItem("好好的事实的话"));
		menu.getItems().add(new MenuItem("好好的事实的话"));
		menu.getItems().add(new MenuItem("好好的事实的话"));
		menu.getItems().add(new MenuItem("好好的事实的话"));
		menu.getItems().add(new MenuItem("好好的事实的话"));

		SystemTray tray = SystemTray.getSystemTray();
		BufferedImage image = ImageIO.read(OnlyTrayIconDemo.class.getResourceAsStream("tray.png"));
		// Image image = new
		// ImageIcon("Resources/Images/Logo/logo_16.png").getImage();
		trayIcon = new OnlyTrayIcon(image, "自动备份工具");
		trayIcon.setToolTip("自动备份工具");
		trayIcon.setContextMenu(menu);
		tray.add(trayIcon);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: TrayViewImpl.java    From oim-fx with MIT License 5 votes vote down vote up
private void initTray() {
	try {
		if (SystemTray.isSupported()) {
			SystemTray tray = SystemTray.getSystemTray();
			tray.add(trayIcon);
		}
	} catch (AWTException ex) {
		ex.printStackTrace();
	}
}
 
Example 5
Source File: Manager.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private void start(String[] args) {
    SystemTray tray = SystemTray.getSystemTray();
    TrayIcon icon = new TrayIcon(Toolkit.getDefaultToolkit().createImage(
            getClass().getResource(
                    "/com/ramussoft/gui/server/application.png")),
            getString("Server") + " " + Metadata.getApplicationName(),
            createPopupMenu());
    icon.setImageAutoSize(true);
    try {
        tray.add(icon);
    } catch (AWTException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: Boot.java    From MakeLobbiesGreatAgain with MIT License 5 votes vote down vote up
public static void setupTray() throws AWTException {
	final SystemTray tray = SystemTray.getSystemTray();
	final PopupMenu popup = new PopupMenu();
	final MenuItem info = new MenuItem();
	final MenuItem exit = new MenuItem();
	final TrayIcon trayIcon = new TrayIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), "MLGA", popup);
	try {
		InputStream is = FileUtil.localResource("icon.png");
		trayIcon.setImage(ImageIO.read(is));
		is.close();
	} catch (IOException e1) {
		e1.printStackTrace();
	}

	info.addActionListener(e -> {
		String message = "Double-Click to lock/unlock the overlay for dragging";
		JOptionPane.showMessageDialog(null, message, "Information", JOptionPane.INFORMATION_MESSAGE);
	});

	exit.addActionListener(e -> {
		running = false;
		tray.remove(trayIcon);
		ui.close();
		System.out.println("Terminated UI...");
		System.out.println("Cleaning up system resources. Could take a while...");
		handle.close();
		System.out.println("Killed handle.");
		System.exit(0);
	});
	info.setLabel("Help");
	exit.setLabel("Exit");
	popup.add(info);
	popup.add(exit);
	tray.add(trayIcon);
}
 
Example 7
Source File: SysTray.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void hideTrayIcon() {
    SystemTray tray = SystemTray.getSystemTray();
    if (tray != null) {
        try {
            tray.remove(trayIcon);
        } catch (Exception e) {
            Exceptions.printStackTrace(e);
        }
    }
    trayIcon = null;
}
 
Example 8
Source File: SwingUtil.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create tray icon.
 *
 * @param icon  the icon
 * @param title the title
 * @param frame the frame
 * @return the tray icon
 */
@Nullable
public static TrayIcon createTrayIcon(@Nonnull final Image icon, @Nonnull final String title, @Nonnull final Frame frame)
{
	if (!SystemTray.isSupported())
	{
		return null;
	}

	final SystemTray systemTray = SystemTray.getSystemTray();
	final TrayIcon trayIcon = new TrayIcon(icon, title);
	trayIcon.setImageAutoSize(true);

	try
	{
		systemTray.add(trayIcon);
	}
	catch (AWTException ex)
	{
		log.debug("Unable to add system tray icon", ex);
		return trayIcon;
	}

	// Bring to front when tray icon is clicked
	trayIcon.addMouseListener(new MouseAdapter()
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			frame.setVisible(true);
			frame.setState(Frame.NORMAL); // Restore
		}
	});

	return trayIcon;
}
 
Example 9
Source File: AlertMaker.java    From Library-Assistant with Apache License 2.0 5 votes vote down vote up
public static void showTrayMessage(String title, String message) {
    try {
        SystemTray tray = SystemTray.getSystemTray();
        BufferedImage image = ImageIO.read(AlertMaker.class.getResource(LibraryAssistantUtil.ICON_IMAGE_LOC));
        TrayIcon trayIcon = new TrayIcon(image, "Library Assistant");
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("Library Assistant");
        tray.add(trayIcon);
        trayIcon.displayMessage(title, message, MessageType.INFO);
        tray.remove(trayIcon);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
 
Example 10
Source File: WindowGui.java    From winthing with Apache License 2.0 5 votes vote down vote up
public void setIcon(boolean color) {
    SystemTray tray = SystemTray.getSystemTray();
    TrayIcon[] icons = tray.getTrayIcons();
    if (icons.length > 0) {
        String name = color ? "favicon-green.png" : "favicon-red.png";

        URL url = getClass().getClassLoader().getResource(name);
        Image image = Toolkit.getDefaultToolkit().getImage(url);

        int trayWidth = tray.getTrayIconSize().width;
        int trayHeight = tray.getTrayIconSize().height;
        Image scaled = image.getScaledInstance(trayWidth, trayHeight, Image.SCALE_SMOOTH);
        icons[0].setImage(scaled);
    }
}
 
Example 11
Source File: Main.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public static void removeTrayIcon() {
    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();
        if (trayIcon != null) {
            tray.remove(trayIcon);
            trayIcon = null;
        }
    }
}
 
Example 12
Source File: SysTrayPlugin.java    From Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void shutdown() {
	if (SystemTray.isSupported()) {
		SystemTray tray = SystemTray.getSystemTray();
		tray.remove(trayIcon);
	}
	ChatManager.getInstance().removeChatMessageHandler(chatMessageHandler);
}
 
Example 13
Source File: App.java    From img2latex-mathpix with Apache License 2.0 4 votes vote down vote up
/**
 * Tray icon handler.
 */
private void trayIconHandler(InputStream iconInputStream) throws IOException, AWTException {

    // set up the system tray
    var tray = SystemTray.getSystemTray();
    var image = ImageIO.read(iconInputStream);
    // use the loaded icon as tray icon
    var trayIcon = new TrayIcon(image);

    // show the primary stage if the icon is right clicked
    trayIcon.addActionListener(event -> Platform.runLater(this::showStage));

    // add app name as a menu item
    var openItem = new MenuItem(APPLICATION_TITLE);
    // show the primary stage if the app name item is clicked
    openItem.addActionListener(event -> Platform.runLater(this::showStage));

    // add Preferences menu item
    var settingItem = new MenuItem("Preferences");
    settingItem.addActionListener(event -> Platform.runLater(() -> UIUtils.showPreferencesDialog(0)));

    // add check for updates menu item
    var updateCheckItem = new MenuItem("Check for Updates");

    // add current version info menu item
    var versionItem = new MenuItem("Version: " + PROPERTIES.getProperty("version"));

    // add click action listener
    updateCheckItem.addActionListener(event -> {
        try {
            Desktop.getDesktop().browse(new URI(IOUtils.GITHUB_RELEASES_URL));
        } catch (IOException | URISyntaxException ignored) {
        }
    });

    // add quit option as the app cannot be closed by clicking the window close button
    var exitItem = new MenuItem("Quit");

    // add action listener for cleanup
    exitItem.addActionListener(event -> {
        // remove the icon
        tray.remove(trayIcon);

        Platform.exit();
        System.exit(0);
    });

    // set up the popup menu
    final var popup = new PopupMenu();
    popup.add(openItem);
    popup.addSeparator();
    popup.add(settingItem);
    popup.addSeparator();
    popup.add(versionItem);
    popup.add(updateCheckItem);
    popup.addSeparator();
    popup.add(exitItem);
    trayIcon.setPopupMenu(popup);

    // add icon to the system
    tray.add(trayIcon);

}
 
Example 14
Source File: TrayManager.java    From desktopclient-java with GNU General Public License v3.0 4 votes vote down vote up
void removeTray() {
    if (mTrayIcon != null) {
        SystemTray tray = SystemTray.getSystemTray();
        tray.remove(mTrayIcon);
    }
}