Reputation: 544
I'm building a small program that's got a settings screen where the user can choose a color theme. I used a JRadioButton for every color but I can't get the program to save it for the next time I run the program. Here's my code:
private Preferences userPreferences = Preferences.userRoot();
MainWindow() {
super("Timer");
setLayout(new GridLayout(4,3,5,5));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(650,290);
setResizable(false);
userPreferences.get("COLOR_CODE", "#ededed");
//MORE CODE HERE
@Override
public void menuSelected(MenuEvent e) {
//Open settings window
if(e.getSource().equals(settings)) {
about.setEnabled(false);
exit.setEnabled(false);
settingsFrame = new SettingsWindow();
settingsFrame.setAlwaysOnTop(true);
settingsFrame.setLocationRelativeTo(null);
settingsFrame.setVisible(true);
//WindowListener for closing the settings window
settingsFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
settings.setEnabled(true);
about.setEnabled(true);
exit.setEnabled(true);
}
});
//Adding action listeners to the radio buttons
settingsFrame.defaultColorRB.addActionListener(ee -> {
settingsFrame.setColor("#ededed");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.whiteColorRB.addActionListener(ee -> {
settingsFrame.setColor("#FFFFFF");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.lightGrayColorRB.addActionListener(ee -> {
settingsFrame.setColor("#D2D8DF");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.darkGrayColorRB.addActionListener(ee -> {
settingsFrame.setColor("#A2A4A6");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.yellowColorRB.addActionListener(ee -> {
settingsFrame.setColor("#FBFF00");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.pinkColorRB.addActionListener(ee -> {
settingsFrame.setColor("#F58EB3");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.cyanColorRB.addActionListener(ee -> {
settingsFrame.setColor("#32D0F7");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
}
}
Please can anyone let me know why the code above doesn't save the user's choice of color?
Upvotes: 0
Views: 194
Reputation: 29680
If saving the color code, as posted in the question, you will need to test the saved color code and select the respective button. Example, very basic, just for demonstration:
// assuming userPreferences is java.util.prefs.Preferences
String colorCode = userPreferences.get("COLOR_CODE", "#ededed");
switch (colorCode.toLowerCase()) {
case "#ededed": defaultColorRB.setSelected(true); break;
case "#ffffff": whiteColorRB.setSelected(true); break;
...
default: /* none selected - not sure what you want to do in that case */ break;
}
Hint 1: use constants for the preferences key "COLOR_CODE" and the colors (e.g. "ededed")
Hint 2: since you are using radio buttons, that is, some distinct colors, it would be easier save the selected radio button index (or some constant) instead of the color code itself (e.g. #ededed). If the user is allowed to select ANY color, not only the radio buttons ones, you sure must save the color code.
Here an example for the second hint:
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class Radio {
private static final String PREF_BUTTON_INDEX = "color button";
private final Preferences prefs = Preferences.userNodeForPackage(getClass());
private static final String[] COLORS = { "#ededed", "#ffffff", "#a00000" };
private List<JRadioButton> buttons;
private JLabel output; // so we can see something, simulating usage of color
private Radio() {
buttons = new ArrayList<>();
SwingUtilities.invokeLater(this::initGUI);
}
private void initGUI() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < COLORS.length; i++) {
JRadioButton button = new JRadioButton(COLORS[i]);
button.addActionListener(this::buttonSelected);
group.add(button);
panel.add(button);
buttons.add(button);
}
output = new JLabel("undefined");
output.setBorder(new TitledBorder("Color:"));
panel.add(output);
int colorIndex = prefs.getInt(PREF_BUTTON_INDEX, -1);
if (colorIndex != -1) {
buttons.get(colorIndex).setSelected(true);
output.setText(COLORS[colorIndex]);
}
JOptionPane.showMessageDialog(null, panel);
}
private void buttonSelected(ActionEvent ev) {
int index = buttons.indexOf(ev.getSource());
if (index != -1) {
output.setText(COLORS[index]);
prefs.putInt(PREF_BUTTON_INDEX, index);
}
}
public static void main(String[] args) {
new Radio();
}
}
Upvotes: 1
Reputation: 312
One good usage first is to create a node and not to save the preferences directly in the user root node. Try this:
// This will define a node in which the preferences can be stored
prefs = Preferences.userRoot().node(this.getClass().getName());
And see if this changing something.
Upvotes: 0