@Override public Dimension iconSize() { return SystemTray.getSystemTray().getTrayIconSize(); }
if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Dimension trayIconSize = tray.getTrayIconSize(); // resize icon image to trayIconSize // create your tray icon off of the resized image }
public Image imageForTray(SystemTray theTray){ Image trayImage = Toolkit.getDefaultToolkit().getImage("my100x100icon.png"); Dimension trayIconSize = theTray.getTrayIconSize(); trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH); return trayImage; }
/** * Choose a size to use for the icons in the system tray based on the size the system tray requests. * * <ul> * <li>Windows: 16x16px</li> * <li>Mac OS X: 20x20px</li> * <li>Gnome: 24x24px</li> * <li>KDE: 24x24</li> * <li>Others: 32x32, with scaling applied.</li> * </ul> * * @param sysTray The system tray. * @return The best icon size. */ private StatusIconSize chooseIconSize(final SystemTray sysTray) { final Dimension trayIconSize = sysTray.getTrayIconSize(); if (trayIconSize.getHeight() == 16) { return StatusIconSize.SIZE_16x16; } else if (trayIconSize.getHeight() == 20) { return StatusIconSize.SIZE_20x20; } else if (trayIconSize.getHeight() == 24) { return StatusIconSize.SIZE_24x24; } else { return StatusIconSize.SIZE_32x32; } }
private TrayIcon getTrayIcon(NotificationType notificationType) throws IOException { Dimension size = tray().getTrayIconSize(); String imageName = null; switch (notificationType) { case SUCCESS: imageName = "/icons/ok.png"; break; case FAIL: imageName = "/icons/error.png"; break; } BufferedImage icon = ImageIO.read(getClass().getResourceAsStream(imageName)); return new TrayIcon(icon.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH)); }
final Dimension trayIconSize = systemTray.getTrayIconSize();
public void install() { try { if (trayIcon == null && SystemTray.isSupported()) { SystemTray systemTray = SystemTray.getSystemTray(); Dimension size = systemTray.getTrayIconSize(); trayIcon = createTrayIcon(size); systemTray.add(trayIcon); JPopupMenu popup = new JPopupMenu(); trayIcon.setJPopupMenu(popup); createPopup(popup); } } catch (AWTException e) { e.printStackTrace(); } }
Dimension trayIconSize = systemTray.getTrayIconSize();