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

The following examples show how to use java.awt.SystemTray#getTrayIconSize() . 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: 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);
    }
}