Guest User

Untitled

a guest
Mar 2nd, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class SystemTrayManager
  2. {
  3. private SystemTray systemTray = SystemTray.getSystemTray();
  4. private Toolkit toolkit = Toolkit.getDefaultToolkit();
  5. private PopupMenu popMenu = new PopupMenu();
  6. private TrayIcon trayIcon;
  7.  
  8.  
  9. public SystemTrayManager() throws IOException
  10. {
  11. this.trayIcon = new TrayIcon(setIconImage("trayIcon.png"), "POD BOT");
  12. runTray();
  13. }
  14.  
  15.  
  16. private void runTray()
  17. {
  18. if(!SystemTray.isSupported())
  19. {
  20. System.out.println("System tray is not supported!");
  21. return;
  22. }
  23.  
  24. MenuItem exit = new MenuItem("Exit");
  25.  
  26. exit.addActionListener(new ActionListener() {
  27. @Override
  28. public void actionPerformed(ActionEvent e) {
  29. System.exit(0);
  30. }
  31. });
  32.  
  33. popMenu.add(exit);
  34.  
  35. trayIcon.setPopupMenu(popMenu);
  36.  
  37. try {
  38. systemTray.add(trayIcon);
  39. } catch (AWTException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43.  
  44. private Image setIconImage(String imagePath)
  45. {
  46. return toolkit.getImage(imagePath);
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment