0

How can i set an Icon for all frames that will exist in my application? I don't want to use frame.setIconImage(0); frame1.setIconImage(1); etc. every time i create a frame.

Best Regards, Marek

Marek
  • 1,189
  • 3
  • 13
  • 33

2 Answers2

1

make a class which extends jframe .

public class customFrame extends JFrame{

    public customFrame() {
        try {
            this.setIconImage(ImageIO.read(new File("C:\\Users\\Madhawa.se\\Desktop\\gtg.PNG")));
        } catch (IOException ex) {
            Logger.getLogger(customFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}

now when you create jframes extends that class[frame] instead jframe.

like follow ;

public class frame1 extends customFrame{
    public static void main(String[] args) {
        new frame1().setVisible(true);

    }
}

next frame so on..

public class frame2 extends customFrame{
    public static void main(String[] args) {
        new frame2().setVisible(true);

    }
}

and also you can make frames like follow

public static void main(String[] args) {
    customFrame frame1 = new customFrame();
    frame1.setTitle("title");
    frame1.add(new JLabel(""));
    frame1.setVisible(true);
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
0

While you could extend JFrame and create a custom implementation, you're not really adding much new functionality to the frame , which could come, back and haunt you latter.

Another approach would be to create a frame factory, which could be used to generate the frame the way you want it. This would allow you to create a series of methods to meet your needs.

It also means you can change the way the factory methods work without having to change any of the other code...

public class FrameFactory {
    public static JFrame createDefaultFrame() {
        JFrame frame = new JFrame();
        frame.setIconImage(ImageIO.read(...)));
        return frame;
    }

    public static JFrame createFrameWithTitle(String title) {
        JFrame frame = FrameFactory.createDefaultFrame();
        frame.setTitle(title);
        return frame;
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366