0

I'm currently trying to create a method to change all of my jFrames' icons from a single, separate class (in a different package) at once, so that I don't need to add the icon change method to EVERY jFrame class, and instead just having to copy one or two lines of code and change the target jFrame.

But I can't seem to actually make it work, regardless of the way I try. Here's the code for the class that is supposed to change the icon:

package com.sts.images;

import com.sts.screens.LoginScreen;
import javax.swing.ImageIcon;

public class ChangeIcon{

    LoginScreen LS = new LoginScreen();

    ImageIcon IC = new ImageIcon("images/icon.png");

    LoginScreen.setIconImage(IC.getImage()); //First thing I tried...

    LS.setIconImage(IC.getImage()); //Second thing I tried....

}

(NOTE: The "LoginScreen" jFrame is located in another package within the same project.)

Is it possible at all to do such a thing? Or should I just add it to every jFrame instead?

PolarDog
  • 33
  • 2
  • 7
  • 1
    Use meaningful variable names (`LS` tells me nothing, same for `IC`), and follow [Java naming conventions](https://www.oracle.com/technetwork/java/codeconventions-135099.html) `firstWordLowerCaseVariable`, `firstWordLowerCaseMethod`, `FirstWordUpperCaseClass` and `ALL_WORDS_UPPERCASE_CONSTANT`. We don't know how `LoginScreen` looks like, and why is `setIconImage` a static method? (Just guessing here due to how you call the method). Is that a method of yours? For better help sooner post a proper [mcve] – Frakcool May 10 '19 at 18:58
  • 1
    Btw you should only have 1 single `JFrame`, see [The use of multiple JFrames, good / bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) The general consensus says it's bad, instead build your UI using multiple `JFrames`, `JDialogs` and / or `CardLayout` to switch between different views – Frakcool May 10 '19 at 19:00
  • The code posted can not compile. Ignoring it, it is not clear what you are trying to do. Are you trying to 1. Change the icons of every `LoginScreen` instance ? **OR** 2. Have a utility to change the icon of different `JFrame` s (for example `LoginScreen` , `LogoutScreen` and others) ? – c0der May 11 '19 at 07:44
  • Yes, I am already aware that the code doesn't compile, that's why I came here. And what I want is 2, an utility to be change the icons of ALL jFrame screens at once. – PolarDog May 11 '19 at 21:24

0 Answers0