Changing LAF PopupMenu for TrayIcon in Java

I have the following code to create TrayIcon with PopupMenu:

public void addToTray() { try { try { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } PopupMenu popMenu= new PopupMenu(); MenuItem exititem = new MenuItem("Exit"); popMenu.add(exititem); BufferedImage trayImg = ImageIO.read(new File("Geqo.png")); ImageIcon ii = new ImageIcon(trayImg); TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", popMenu); trayIcon.setImageAutoSize(true); SystemTray.getSystemTray().add(trayIcon); } catch (Exception e) { e.printStackTrace(); } } 

This code is for creating TrayIcon with PopupMenu. It works great. But I did not like the standard LAF (Metal, I think). So I tried changing LAF to nimbus, as well as OS Default, Windows, but to no avail. LAF does not seem to change. Can someone impose on me how I can change LAF? Thank you in advance:)!!

+4
source share
1 answer

Popup not a Swing component (therefore, it does not fall under the control of the LookAndFeel manager).

Popup is an AWT component that typically uses native components.

Instead, you should try something more similar ...

 public void addToTray() { try { try { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } BufferedImage trayImg = ImageIO.read(new File("Geqo.png")); ImageIcon ii = new ImageIcon(trayImg); final TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", null); JPopupMenu jpopup = new JPopupMenu(); JMenuItem miExit = new JMenuItem("Exit"); jpopup.add(miExit); miExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { SystemTray.getSystemTray().remove(trayIcon); System.exit(0); } }); trayIcon.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { jpopup.setLocation(e.getX(), e.getY()); jpopup.setInvoker(jpopup); jpopup.setVisible(true); } } }); trayIcon.setImageAutoSize(true); SystemTray.getSystemTray().add(trayIcon); } catch (Exception e) { e.printStackTrace(); } } 

This is based on the idea suggested in Using JPopupMneu in TrayIcon

.......

+3
source

Source: https://habr.com/ru/post/1487681/


All Articles