0

Within my program I have a method to change the frame colours. I also have a method to open a new Jframe which is used as a settings menu for the app. However the values set in the initial jframe will not carry over.

How may I preserve the colours set in the initial Jframe and have them load into the settings object as it is created ?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61

2 Answers2

1

Add a constructor to your new JFrame with a Color parameter and set the background color after you called the default constructor.

public SecondJFrame(Color c)
{
    this();
    this.getContentPane().setBackground(c);
}

Another way is to set the background color after you initialized your second JFrame in your initial JFrame:

SecondJFrame secondJFrame = new SecondJFrame();
secondJFrame.getContentPane().setBackground(this.getContentPane().getBackground());
secondJFrame.setVisible(true);
Stefan
  • 1,433
  • 1
  • 19
  • 30
0

user singlton design pattern add to it the Setting class you have as follow

public class SettingManager{
     private static YourSettingClass setting = null ; 
     private SettingManager(){}
     public static YouSettingClass getSetting(){
              if(setting==null){
                 setting = new YourSettingClass(); 
                 return setting; 
              }
         return setting ; 
     }
     // any utility method to change your setting will be here 

}

in each JFrame Constructor you can get the setting which is now global for the application

YourSettingClass setting = SettingManager.getSetting() ; 
Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
  • Dependency injection ís favorable over the Singleton pattern. It might not be an issue in this case, but Singletons often hide dependencies and it might be a problem to unitary testing. – Renatols Apr 23 '15 at 20:40
  • Still you can use it by Single on YourSettingsClass that the case in spring framework in this case you dont need the manager and you will use the class directly by Autwire it – Alaa Abuzaghleh Apr 23 '15 at 21:51