Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Worthy

Save and Load GUI settings

10 posts in this topic

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
1 person likes this

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.

1 person likes this

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

Please help I have dinner bell and get the guide to load preset and it says calling the servant the servant comes and nothing happens

Share this post


Link to post
Share on other sites

Please help I have dinner bell and get the guide to load preset and it says calling the servant the servant comes and nothing happens

I got your PM, although you posted this on the wrong thread.

Share this post


Link to post
Share on other sites

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

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.

1 person likes this

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.