Question

I'm trying to create a button in game where the background color will go from light_gray to dark_gray. However when the application relaunches I have to re select the button to get the color back to dark_gray.

How would I have it so that it saves the color when the application is relaunched?

My code is very simple and is just an action listener on the button which then changes the bg color of selected items.

Ok, I have now had the chance to allow it to create the properties file but one doesn't know how one could store the data. I've seen people have stuff such as 'properties.setProperty("Favorite sport", "Football");' But how could one have this so that it stores the bg color?

windowDark.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) 
                {
                    try {
                        Properties properties = new Properties();
                        properties.setProperty();

                        File file = new File("DarkTheme.properties");
                        FileOutputStream fileOut = new FileOutputStream(file);
                        properties.store(fileOut, "Dark theme background colour");
                        fileOut.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
Was it helpful?

Solution 2

You can store the color as an int value in the properties file, as follows:

windowDark.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        getProperties().setProperty("color", Integer.toString(getColor().getRGB()));
    }
});

Have the properties as a member of the window this button is in, or even better, in some general location of the application (the class with the main() perhaps ?), and access it with getProperties().

When you need to use the color, parse the string:

Color color = new Color(Integer.parseInt(getProperties().getProperty("color")));

Don't save the properties file on each button click, instead, do so when the application is about to exit:

mainWindow.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
mainWindow.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        try {    
            File file = new File("DarkTheme.properties");
            FileOutputStream fileOut = new FileOutputStream(file);
            getProperties().store(fileOut, "Dark theme background colour");
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            mainWindow.dispose();
        }
    }
});

OTHER TIPS

The java.util.prefs Preferences API is well suited for storing persistent preference data for user applications running on the desktop.

Here's an example how you can use it to store and retrieve persistent background color settings:

import java.awt.Color;
import java.util.prefs.Preferences;

public class MyPrefs {
    private static Preferences preferences = 
                     Preferences.userRoot().node("myappname.ColorPreferences");

    public static void storeBackground(Color background) {
        preferences.putInt("background", background.getRGB());
    }

    public static Color retrieveBackground() {
        // Second argument is the default when the setting has not been stored yet
        int color = preferences.getInt("background", Color.WHITE.getRGB()); 
        return new Color(color);
    }
}

To call it, use something like:

public static void main(String[] args) {
    System.out.println("Background: " + retrieveBackground());
    storeBackground(Color.RED);
}

The change done in memory will be disposed once the application is terminated. If you want to persist some data (in this case, the background color), then you need to store is somewhere, e.g. file, database, etc. For a simple application, storing your data in a file will be practical. To do this, you will need to: - when application starts, read the file, and apply the color specified in the file - while the application is running and user changes the color, save the color to the same file To deal with file, you will need to use File, FileReader, and FileWriter classes (all are in java.io package).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top