Reputation: 21
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 ?
Upvotes: 0
Views: 97
Reputation: 1009
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() ;
Upvotes: 0
Reputation: 1433
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);
Upvotes: 1