简体   繁体   中英

How to load saved item from JSON file in java

I am making an app where user can change theme (dark and light mood) as per his convenience. And the theme that the user chooses will be saved and the saved theme will be there when the app is opened again later.

I have saved the data to file using JSON . And when the user clicks on the theme change button, the data will be written to the file .

The code of the theme:

private void darkTheme() {
    FlatDarkLaf.setup(); 
    UIManager.put("TextField.foreground", new ColorUIResource(Color.WHITE));
    UIManager.put("Label.foreground", new ColorUIResource(Color.WHITE));
    UIManager.put("Button.foreground", new ColorUIResource(Color.WHITE));               
    SwingUtilities.updateComponentTreeUI(contentPane);
    for(int i=0; i<arList.size(); i++) {
        ((JLabel)arList.get(i).getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
    }
}

And the Code for the theme change and write to the file button:

btnDark.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String path = "resources/config.cfg";
            JSONObject json = new JSONObject();
            try {
                json.put("Theme", "darkTheme();");
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
     
            try (PrintWriter out = new PrintWriter(new FileWriter(path))) {
                out.write(json.toString());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            darkTheme();
        }
    });

I can read the file but can't Load the save data. Here the code for read the file:

private void readData() {
    try {
    String path = "resources/config.cfg";
        InputStream is = Button.class.getResourceAsStream(path);
        if (is == null) {
            throw new NullPointerException("Cannot find resource file " + path);
        }
        JSONTokener tokener = new JSONTokener(is);
        JSONObject object = new JSONObject(tokener);
       // object.getString("darkTheme();");
        object.getJSONObject("Theme");
    }
    catch (Exception e) {
        
    }
}

Can anyone please help me how to do this correctly.

Ok Finally I've got a solution by my own and someone's idea. I have changed the line in btnDark

json.put("Theme", "darkTheme();");

to

json.put("Theme", "Dark");

and in the readData() method I write like this

private void readData() {
    String jsonText;
    
    try {
        String path = "resources/config.cfg";
        jsonText = IOUtils.toString(new FileInputStream(new File(path)));
        int i = jsonText.indexOf("{");
        jsonText = jsonText.substring(i);
        JSONObject ob = new JSONObject(jsonText);
        String Theme = ob.getString("Theme");
        if(Theme.equals("Dark")) {
            darkTheme();
        }
        else if(Theme.equals("Light")) {
            lightTheme();
        }
        
    }catch(Exception ex) {
        ex.printStackTrace();
    }
}

And now it's work perfectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM