Is there an alternative to getClass (). GetResource () for Static ImageIcon

Ok, so I got a static ImageIcon and the image just doesn't appear. In the same program, I use other ImagesIcon, but they are not static, so when I declare them, I do it as follows:

public ImageIcon blabla = new ImageIcon(getClass().getResource(blabla.png)); 

But if I declare ImageIcon Static, I cannot use this line because you cannot access getClass () from a static value. Right now, these images are not displayed using this:

 public static ImageIcon blabla = new ImageIcon(blabla.png); 

Thanks for your help!

 public static ImageIcon networkOfflineIcon = new ImageIcon("Images/networkOfflineIcon.png"); public static ImageIcon networkIcon = new ImageIcon("Images/networkIcon.png"); protected static JMenuItem jmiRemote = new JMenuItem(" Remote", networkOfflineIcon); //************************************************************************ public static void changeNetWorkStatus(boolean network_status) //************************************************************************ { if(network_status){ Application.jmiRemote.setIcon(networkIcon); Application.jmiRemote.setText("NetWork Online/Remote is On"); Application.lockScreenRemote(); }else if(!network_status){ Application.jmiRemote.setIcon(networkOfflineIcon); Application.jmiRemote.setText("NetWork Offline/Remote is Off"); Application.unlockScreenRemote(); } }//DOESNT CHANGE THE IMAGE //************************************************************************ 
+6
source share
1 answer

In a static context, you can write:

 public ImageIcon imageIcon = new ImageIcon(MyClass.class.getResource("icon.png")); 

Or, try ImageIO.read(new File("icon.png"))

+4
source

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


All Articles