0

I am trying to do the simple activity of setting the Application Icon in a java application. I have many working examples with me but in this instance it fails. Please help I have tried a) b) c) marked in the code. a) Gives a error hint 'Non-static getClass cannot be referenced from a static context' So I tried b) and c). In both, the program runs, but NO Icon is set, NO errors. (I have put the same image in different paths for test purpose)

private static void createAndShowGUI() {

        myFrame = new MyDynamic();
        myFrame.setTitle( "Sunsong Public School : Home" );
        a)myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../Images/Sudan.png")));
        b)myFrame.setIconImage(ImageIO.read(new File("../Images/Sudan.png")));
        c)Image icoon = Toolkit.getDefaultToolkit().getImage("Sudan.png"
Padmanabhan
  • 152
  • 2
  • 14

1 Answers1

0

Got the solution from some other source. The funny thing is I have dozens of working Applications with Icon. In all of them I am using the Toolkit-getclass-getresources. But in this case I was tinkering with an old code, and stuck inside a static block. From the static context Calling toolkits non-static getClass is NOT working. Had tried hundreds of examples from the net in the last 20 or so hours. Java IO also NOT working complains cannot 'read from file'.

This is the one Finally worked..

ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("Sudan.png"));
Image img = icon.getImage();
myFrame.setIconImage(img); // frame is a JFrame
Padmanabhan
  • 152
  • 2
  • 14