Jump to content
Worthy

Save and Load GUI settings

Recommended Posts

Save and Load GUI Settings Tutorial

by Worthy

 
In this tutorial
We will be using java.util Propteries class to save and load settings from your GUI (or anything else that is applicable)

________
Adding the proper variables for preparation.
Before you start we'll need a Properties file and the file that saves the settings.
In your Variables class or Main class add:

 
public static Properties prop = new Properties();
public static File path;

 

When you start the script, we must declare the path variables:

 
//The file could be called anything; this is just how I name it
//however this will only work if the player is logged in, so double check for that

Variables.path = new File(Util.getWorkingDirectory().getAbsolutePath(), "scriptname_" + Player.getRSPlayer().getName() + "settings.ini"); 

 

In your GUI class we must create the Save and Load settings methods

 
private void saveSettings() {
    try {
        Variables.prop.clear();    //clear settings everytime
        Variables.prop.put("useAntiban", String.valueOf(useAntiban.isSelected()));    //example of JCheckBox
        Variables.prop.put("woodTypeCombobox", woodTypeCombobox.getSelectedItem());    //example of JComboBox
        Variables.prop.put("logPrice", String.valueOf(logPrice.getValue()));    //example of JSpinner
        Variables.prop.store(new FileOutputStream(Variables.path), "GUI Settings");
    } catch (Exception e1) {
        System.out.print("Unable to save settings");
        e1.printStackTrace();
    }
}

public void loadSettings() {   //we will call this externally    
    try {      
        if (!Variables.path.exists()) {         //make sure file exists          
            Variables.path.createNewFile();      //or make a new one        
        }      
        Variables.prop.load(new FileInputStream(Variables.path));      
        String[] buttonNames = {"useAntiban"};   //helpful if all the names match         
        JCheckBox[] boxes = {useAntiban};      
        for (int i = 0; i < buttonNames.length; i++) {         
            String value = Variables.prop.getProperty(buttonNames[i]);         
            boxes[i].setSelected(Boolean.valueOf(value));   //load JCheckBoxes            //note you must convert the String to a boolean      
        }      
        String[] comboxNames = {"woodTypeCombobox"};      
        JComboBox[] combos = {woodTypeCombobox};      
        for (int i = 0; i < comboxNames.length; i++) {         
            String value = Variables.prop.getProperty(comboxNames[i]);         
            if (value != null) {           
                combos[i].setSelectedItem(value);         //load JComboBoxes                //note no conversion to an Object is necessary because a String is already an Object            
            }      
        }
        String[] spinnerNames = {"logPrice"};         
        JSpinner[] spinners = {logPrice};      
        for (int i = 0; i < spinnerNames.length; i++) {         
            String value = Variables.prop.getProperty(spinnerNames[i]);         
            if (spinnerNames != null) {            
                spinners[i].setValue(Integer.parseInt(value));   //load JSpinners                //must take ints            
            }      
        }   
    } catch (Exception e2) {      
        System.out.print("Unable to load settings");      
        e2.printStackTrace();   
    }
}

 

 

Implementing the methods
On the "Start" button, we will call saveSettings();
On the script initiation we will call the loadSettings(); (after the GUI has been loaded)
 
-->Alternatively you can just create seperate save and load buttons on the GUI.


Tying everything together:
The first thing the script will do is declare the File Path, and initiate the Preferences file.
When the GUI loads, the script will then call loadSettings(), loading the settings (if any).
If no settings are saved, it well then create a new file in the path.
Right before the GUI is disposed it will save all the settings the user has applied in TRiBot's storage directory. (You can print out the location if you don't know where that is for your OS).

_________
Example script on GitHub: https://github.com/WorthyTRiBot/GUISettings
I put everything in one class for the sake of simplicity.


Regards,
Worthy

Edited by Worthy
  • Like 2

Share this post


Link to post
Share on other sites
Properties props = new Properties();File file = new File(Util.getWorkingDirectory().getAbsolutePath() + "name.ini");props.load(new FileReader(file));props.load(new FileWriter(file), "");

Use props like you would a HashMap of type <String, String>, that is all you need.

  • Like 1

Share this post


Link to post
Share on other sites
Properties props = new Properties();File file = new File(Util.getWorkingDirectory().getAbsolutePath() + "name.ini");props.load(new FileReader(file));props.load(new FileWriter(file), "");

Use props like you would a HashMap of type <String, String>, that is all you need.

 

I don't see why that is necessary when you can use a FileInputStream.

And yeah you do use it like HashMap with the put and getProperty settings :)

Edited by Worthy

Share this post


Link to post
Share on other sites

I don't see why that is necessary when you can use a FileInputStream.

And yeah you do use it like HashMap with the put and getProperty settings :)

You have extra code in there. FileWriter will just write to that file, and create a new one if one does not exist yet. You also don't need to use clear.

Share this post


Link to post
Share on other sites
On 10/19/2016 at 10:25 PM, OjDidntDoIt said:

i purchased script and can't seen to get it to work can't save a preset for it to start 

 

 

1 hour ago, dazarius said:

i purchased script can get it to work will only list what to do in text no green click here stuff or ground positioning

This is a programming tutorial on how to save GUI settings.
If you need help with a specific script, post your issue on the script's thread for assistance.

  • Like 1

Share this post


Link to post
Share on other sites
53 minutes ago, Shockplug said:

Very nice tutorial! If you want to write an object to file using props does it have to be a string? 

If you would like to store a java object then I recommend looking up Serialization.

If you want to store something using properties it does not have to be a string, just any nonnull object will do. Properties are just a key-value pair mapping between objects.

Share this post


Link to post
Share on other sites
On 3/7/2017 at 8:34 PM, Worthy said:

If you would like to store a java object then I recommend looking up Serialization.

If you want to store something using properties it does not have to be a string, just any nonnull object will do. Properties are just a key-value pair mapping between objects.

Yea, I was thinking I would just use serialization, thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

×