Jump to content

GUI Saving [SNIPPET]


Dark Magician

Recommended Posts

Hello if you are wondering how to do this, here we go.

 

Example:

  public class HillGiantMagicianGUI extends JFrame {
        Settings prop = new Settings(new File(System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator), this, myPlayer().getName());
        public HillGiantMagicianGUI() {
            initComponents();
            prop.load();
        }

    private void button1ActionPerformed(ActionEvent e) {
  prop.save();
}
  public static class Settings {

        private File PATH_FILE;
        private String FILE_NAME = "SETTINGS.ini";
        private HillGiantMagicianGUI gui;
        private final Properties prop = new Properties();

        public Settings(File f, HillGiantMagicianGUI gui, String accountName) {
            if (!f.exists()) {
                f.mkdirs();
            }
            FILE_NAME = accountName.toUpperCase() + "_" + FILE_NAME;
            PATH_FILE = new File(f, FILE_NAME);
            this.gui = gui;
        }

        public synchronized void save() {
            try {
                if (!PATH_FILE.exists() && !PATH_FILE.createNewFile()) {
                    return;
                }
                if (!PATH_FILE.canWrite()) {
                    PATH_FILE.setWritable(true);
                }
                prop.clear();

                prop.put("I1", gui.comboBox1.getSelectedItem());
                prop.put("T2", gui.textField2.getText());

                prop.put("I2", gui.comboBox2.getSelectedItem());
                prop.put("T3", gui.textField3.getText());

                prop.put("I3", gui.comboBox3.getSelectedItem());
                prop.put("T4", gui.textField4.getText());

                prop.put("I4", gui.comboBox4.getSelectedItem());
                prop.put("T5", gui.textField5.getText());

                prop.put("I7", gui.comboBox7.getSelectedItem());
                prop.put("T6", gui.textField6.getText());

                prop.put("I5", gui.comboBox5.getSelectedItem());
                prop.put("T7", gui.textField7.getText());

                prop.put("I6", gui.comboBox6.getSelectedItem());
                prop.put("T8", gui.textField8.getText());

                prop.put("I8", gui.comboBox8.getSelectedItem());
                prop.put("T9", gui.textField9.getText());

                prop.put("c1", String.valueOf(gui.checkBox1.isSelected()));
                prop.put("c2", String.valueOf(gui.checkBox2.isSelected()));
                prop.put("c3", String.valueOf(gui.checkBox3.isSelected()));
                prop.put("c4", String.valueOf(gui.checkBox4.isSelected()));
                prop.put("c5", String.valueOf(gui.checkBox5.isSelected()));
                prop.put("c6", String.valueOf(gui.checkBox6.isSelected()));

                prop.put("r1", String.valueOf(gui.radioButton1.isSelected()));
                prop.put("r2", String.valueOf(gui.radioButton2.isSelected()));
                prop.put("r3", String.valueOf(gui.radioButton3.isSelected()));
                prop.put("r4", String.valueOf(gui.radioButton4.isSelected()));
                prop.put("r5", String.valueOf(gui.radioButton5.isSelected()));
                prop.put("r6", String.valueOf(gui.radioButton6.isSelected()));
                prop.put("r7", String.valueOf(gui.radioButton7.isSelected()));
                prop.put("r8", String.valueOf(gui.radioButton8.isSelected()));
                prop.put("r9", String.valueOf(gui.radioButton9.isSelected()));
                prop.put("r10", String.valueOf( gui.radioButton10.isSelected()));
                prop.put("r11", String.valueOf(gui.radioButton11.isSelected()));
                prop.put("r12", String.valueOf( gui.radioButton12.isSelected()));

                prop.put("r14", String.valueOf(gui.radioButton14.isSelected()));
                prop.put("r15", String.valueOf(gui.radioButton15.isSelected()));
                prop.put("r16", String.valueOf(gui.radioButton16.isSelected()));
                prop.put("r17", String.valueOf(gui.radioButton17.isSelected()));
                prop.put("r18", String.valueOf(gui.radioButton18.isSelected()));
                prop.put("r19", String.valueOf(gui.radioButton19.isSelected()));
                prop.put("r20", String.valueOf(gui.radioButton20.isSelected()));
                prop.put("r21", String.valueOf(gui.radioButton21.isSelected()));

                prop.store(new FileOutputStream(PATH_FILE), "GUI Settings");
                PATH_FILE.setReadOnly();
            } catch (Throwable e) {
            }
        }

        public synchronized void load() {
            try {
                if (PATH_FILE.exists()) {
                    prop.load(new FileInputStream(PATH_FILE));

                    gui.comboBox1.setSelectedItem(prop.getProperty("I1"));
                    if (prop.getProperty("T2").toString() != "") {
                        gui.textField2.setText(prop.getProperty("T2"));
                    }

                    gui.comboBox2.setSelectedItem(prop.getProperty("I2"));
                    if (prop.getProperty("T3").toString() != "") {
                        gui.textField3.setText(prop.getProperty("T3"));
                    }

                    gui.comboBox3.setSelectedItem(prop.getProperty("I3"));
                    if (prop.getProperty("T4").toString() != "") {
                        gui.textField4.setText(prop.getProperty("T4"));
                    }

                    gui.comboBox4.setSelectedItem(prop.getProperty("I4"));
                    if (prop.getProperty("T5").toString() != "") {
                        gui.textField5.setText(prop.getProperty("T5"));
                    }

                    gui.comboBox7.setSelectedItem(prop.getProperty("I7"));
                    if (prop.getProperty("T6").toString() != "") {
                        gui.textField6.setText(prop.getProperty("T6"));
                    }

                    gui.comboBox5.setSelectedItem(prop.getProperty("I5"));
                    if (prop.getProperty("T7").toString() != "") {
                        gui.textField7.setText(prop.getProperty("T7"));
                    }

                    gui.comboBox6.setSelectedItem(prop.getProperty("I6"));
                    if (prop.getProperty("T8").toString() != "") {
                        gui.textField8.setText(prop.getProperty("T8"));
                    }

                    gui.comboBox8.setSelectedItem(prop.getProperty("I8"));
                    if (prop.getProperty("T9").toString() != "") {
                        gui.textField9.setText(prop.getProperty("T9"));
                    }

                    gui.checkBox1.setSelected(Boolean.valueOf(prop.getProperty("c1", "false")));
                    gui.checkBox2.setSelected(Boolean.valueOf(prop.getProperty("c2", "false")));
                    gui.checkBox3.setSelected(Boolean.valueOf(prop.getProperty("c3", "false")));
                    gui.checkBox4.setSelected(Boolean.valueOf(prop.getProperty("c4", "false")));
                    gui.checkBox5.setSelected(Boolean.valueOf(prop.getProperty("c5", "false")));
                    gui.checkBox6.setSelected(Boolean.valueOf(prop.getProperty("c6", "false")));

                    gui.radioButton1.setSelected(Boolean.valueOf(prop.getProperty("r1", "false")));
                    gui.radioButton2.setSelected(Boolean.valueOf(prop.getProperty("r2", "false")));
                    gui.radioButton3.setSelected(Boolean.valueOf(prop.getProperty("r3", "false")));
                    gui.radioButton4.setSelected(Boolean.valueOf(prop.getProperty("r4", "false")));
                    gui.radioButton5.setSelected(Boolean.valueOf(prop.getProperty("r5", "false")));
                    gui.radioButton6.setSelected(Boolean.valueOf(prop.getProperty("r6", "false")));
                    gui.radioButton7.setSelected(Boolean.valueOf(prop.getProperty("r7", "false")));
                    gui.radioButton8.setSelected(Boolean.valueOf(prop.getProperty("r8", "false")));
                    gui.radioButton9.setSelected(Boolean.valueOf(prop.getProperty("r9", "false")));
                    gui.radioButton10.setSelected(Boolean.valueOf(prop.getProperty("r10", "false")));
                    gui.radioButton11.setSelected(Boolean.valueOf(prop.getProperty("r11", "false")));
                    gui.radioButton12.setSelected(Boolean.valueOf(prop.getProperty("r12", "false")));

                    gui.radioButton14.setSelected(Boolean.valueOf(prop.getProperty("r14", "false")));
                    gui.radioButton15.setSelected(Boolean.valueOf(prop.getProperty("r15", "false")));
                    gui.radioButton16.setSelected(Boolean.valueOf(prop.getProperty("r16", "false")));
                    gui.radioButton17.setSelected(Boolean.valueOf(prop.getProperty("r17", "false")));
                    gui.radioButton18.setSelected(Boolean.valueOf(prop.getProperty("r18", "false")));
                    gui.radioButton19.setSelected(Boolean.valueOf(prop.getProperty("r19", "false")));
                    gui.radioButton20.setSelected(Boolean.valueOf(prop.getProperty("r20", "false")));
                    gui.radioButton21.setSelected(Boolean.valueOf(prop.getProperty("r21", "false")));
                }
            } catch (Throwable e) {}
        }
    }
  • Like 1
Link to comment
Share on other sites

Just saying, people aren't going to have the same GUI options so you want to have an easier way to read/write:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Save {

	private PrintWriter pw;
	
	String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;
	
	public Save(String localName) {
		try {
			pw = new PrintWriter(localDir + localName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public boolean writeString(String s) {
		pw.println(s);
		return true;
	}
	
	public boolean writeObject(Object o) {
		pw.println(String.valueOf(o));
		return true;
	}
	
	public boolean writeBoolean(boolean b) {
		return writeObject(b);
	}
	
	public boolean writeInt(int i) {
		return writeObject(i);
	}
	
	
}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Load {

	private BufferedReader br;
	private InputStream fis;
	private InputStreamReader isr;
	private boolean finished = false;
	
	String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;
	
	public Load(String localName) {
		try {
			fis = new FileInputStream(localDir + localName);
			isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
			br = new BufferedReader(isr);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String readString() throws IOException {
		String line = null;
		if (!finished) {
			line = br.readLine();
			if (line == null) finished = true;
		}
		else {
			if (br != null) {
				br.close();
				fis.close();
				isr.close();
				br = null;
			}
		}
		return line;
	}
	
	public boolean readBoolean() throws IOException {
		return Boolean.valueOf(readString());
	}
	
	
	public int readInt() throws IOException {
		return Integer.valueOf(readString());
	}
}

Then you can just do:

Save s = new Save("settings.ini");
s.writeString("Checkbox 1");

//
Load l = new Load("settings.ini");
String checkbox = l.readString(); //Checkbox 1
Edited by Bobrocket
  • Like 1
Link to comment
Share on other sites

 

Just saying, people aren't going to have the same GUI options so you want to have an easier way to read/write:

package Pickpocketer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Save {

	private PrintWriter pw;
	
	String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;
	
	public Save(String localName) {
		try {
			pw = new PrintWriter(localDir + localName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public boolean writeString(String s) {
		pw.println(s);
		return true;
	}
	
	public boolean writeObject(Object o) {
		pw.println(String.valueOf(o));
		return true;
	}
	
	public boolean writeBoolean(boolean b) {
		return writeObject(b);
	}
	
	public boolean writeInt(int i) {
		return writeObject(i);
	}
	
	
}

package Pickpocketer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Load {

	private BufferedReader br;
	private InputStream fis;
	private InputStreamReader isr;
	private boolean finished = false;
	
	String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;
	
	public Load(String localName) {
		try {
			fis = new FileInputStream(localDir + localName);
			isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
			br = new BufferedReader(isr);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String readString() throws IOException {
		String line = null;
		if (!finished) {
			line = br.readLine();
			if (line == null) finished = true;
		}
		else {
			if (br != null) {
				br.close();
				fis.close();
				isr.close();
				br = null;
			}
		}
		return line;
	}
	
	public boolean readBoolean() throws IOException {
		return Boolean.valueOf(readString());
	}
	
	
	public int readInt() throws IOException {
		return Integer.valueOf(readString());
	}
}

Then you can just do:

Save s = new Save("settings.ini");
s.writeString("Checkbox 1");

//
Load l = new Load("settings.ini");
String checkbox = l.readString(); //Checkbox 1

 

I figured people would change the settings to fit their script, radio buttons, checkboxes etc... 

Link to comment
Share on other sites

I figured people would change the settings to fit their script, radio buttons, checkboxes etc... 

 

This is the snippets section, and while I respect your work, you should try and post snippets that people can expand on at their own leisure :)

Having all of your code makes a good example, although then it's more of a tutorial than a snippet :)

Link to comment
Share on other sites

  • 2 weeks later...

 

Hello if you are wondering how to do this, here we go.

 

Example:

  public class HillGiantMagicianGUI extends JFrame {
        Settings prop = new Settings(new File(System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator), this, myPlayer().getName());
        public HillGiantMagicianGUI() {
            initComponents();
            prop.load();
        }

    private void button1ActionPerformed(ActionEvent e) {
  prop.save();
}
  public static class Settings {

        private File PATH_FILE;
        private String FILE_NAME = "SETTINGS.ini";
        private HillGiantMagicianGUI gui;
        private final Properties prop = new Properties();

        public Settings(File f, HillGiantMagicianGUI gui, String accountName) {
            if (!f.exists()) {
                f.mkdirs();
            }
            FILE_NAME = accountName.toUpperCase() + "_" + FILE_NAME;
            PATH_FILE = new File(f, FILE_NAME);
            this.gui = gui;
        }

        public synchronized void save() {
            try {
                if (!PATH_FILE.exists() && !PATH_FILE.createNewFile()) {
                    return;
                }
                if (!PATH_FILE.canWrite()) {
                    PATH_FILE.setWritable(true);
                }
                prop.clear();

                prop.put("I1", gui.comboBox1.getSelectedItem());
                prop.put("T2", gui.textField2.getText());

                prop.put("I2", gui.comboBox2.getSelectedItem());
                prop.put("T3", gui.textField3.getText());

                prop.put("I3", gui.comboBox3.getSelectedItem());
                prop.put("T4", gui.textField4.getText());

                prop.put("I4", gui.comboBox4.getSelectedItem());
                prop.put("T5", gui.textField5.getText());

                prop.put("I7", gui.comboBox7.getSelectedItem());
                prop.put("T6", gui.textField6.getText());

                prop.put("I5", gui.comboBox5.getSelectedItem());
                prop.put("T7", gui.textField7.getText());

                prop.put("I6", gui.comboBox6.getSelectedItem());
                prop.put("T8", gui.textField8.getText());

                prop.put("I8", gui.comboBox8.getSelectedItem());
                prop.put("T9", gui.textField9.getText());

                prop.put("c1", String.valueOf(gui.checkBox1.isSelected()));
                prop.put("c2", String.valueOf(gui.checkBox2.isSelected()));
                prop.put("c3", String.valueOf(gui.checkBox3.isSelected()));
                prop.put("c4", String.valueOf(gui.checkBox4.isSelected()));
                prop.put("c5", String.valueOf(gui.checkBox5.isSelected()));
                prop.put("c6", String.valueOf(gui.checkBox6.isSelected()));

                prop.put("r1", String.valueOf(gui.radioButton1.isSelected()));
                prop.put("r2", String.valueOf(gui.radioButton2.isSelected()));
                prop.put("r3", String.valueOf(gui.radioButton3.isSelected()));
                prop.put("r4", String.valueOf(gui.radioButton4.isSelected()));
                prop.put("r5", String.valueOf(gui.radioButton5.isSelected()));
                prop.put("r6", String.valueOf(gui.radioButton6.isSelected()));
                prop.put("r7", String.valueOf(gui.radioButton7.isSelected()));
                prop.put("r8", String.valueOf(gui.radioButton8.isSelected()));
                prop.put("r9", String.valueOf(gui.radioButton9.isSelected()));
                prop.put("r10", String.valueOf( gui.radioButton10.isSelected()));
                prop.put("r11", String.valueOf(gui.radioButton11.isSelected()));
                prop.put("r12", String.valueOf( gui.radioButton12.isSelected()));

                prop.put("r14", String.valueOf(gui.radioButton14.isSelected()));
                prop.put("r15", String.valueOf(gui.radioButton15.isSelected()));
                prop.put("r16", String.valueOf(gui.radioButton16.isSelected()));
                prop.put("r17", String.valueOf(gui.radioButton17.isSelected()));
                prop.put("r18", String.valueOf(gui.radioButton18.isSelected()));
                prop.put("r19", String.valueOf(gui.radioButton19.isSelected()));
                prop.put("r20", String.valueOf(gui.radioButton20.isSelected()));
                prop.put("r21", String.valueOf(gui.radioButton21.isSelected()));

                prop.store(new FileOutputStream(PATH_FILE), "GUI Settings");
                PATH_FILE.setReadOnly();
            } catch (Throwable e) {
            }
        }

        public synchronized void load() {
            try {
                if (PATH_FILE.exists()) {
                    prop.load(new FileInputStream(PATH_FILE));

                    gui.comboBox1.setSelectedItem(prop.getProperty("I1"));
                    if (prop.getProperty("T2").toString() != "") {
                        gui.textField2.setText(prop.getProperty("T2"));
                    }

                    gui.comboBox2.setSelectedItem(prop.getProperty("I2"));
                    if (prop.getProperty("T3").toString() != "") {
                        gui.textField3.setText(prop.getProperty("T3"));
                    }

                    gui.comboBox3.setSelectedItem(prop.getProperty("I3"));
                    if (prop.getProperty("T4").toString() != "") {
                        gui.textField4.setText(prop.getProperty("T4"));
                    }

                    gui.comboBox4.setSelectedItem(prop.getProperty("I4"));
                    if (prop.getProperty("T5").toString() != "") {
                        gui.textField5.setText(prop.getProperty("T5"));
                    }

                    gui.comboBox7.setSelectedItem(prop.getProperty("I7"));
                    if (prop.getProperty("T6").toString() != "") {
                        gui.textField6.setText(prop.getProperty("T6"));
                    }

                    gui.comboBox5.setSelectedItem(prop.getProperty("I5"));
                    if (prop.getProperty("T7").toString() != "") {
                        gui.textField7.setText(prop.getProperty("T7"));
                    }

                    gui.comboBox6.setSelectedItem(prop.getProperty("I6"));
                    if (prop.getProperty("T8").toString() != "") {
                        gui.textField8.setText(prop.getProperty("T8"));
                    }

                    gui.comboBox8.setSelectedItem(prop.getProperty("I8"));
                    if (prop.getProperty("T9").toString() != "") {
                        gui.textField9.setText(prop.getProperty("T9"));
                    }

                    gui.checkBox1.setSelected(Boolean.valueOf(prop.getProperty("c1", "false")));
                    gui.checkBox2.setSelected(Boolean.valueOf(prop.getProperty("c2", "false")));
                    gui.checkBox3.setSelected(Boolean.valueOf(prop.getProperty("c3", "false")));
                    gui.checkBox4.setSelected(Boolean.valueOf(prop.getProperty("c4", "false")));
                    gui.checkBox5.setSelected(Boolean.valueOf(prop.getProperty("c5", "false")));
                    gui.checkBox6.setSelected(Boolean.valueOf(prop.getProperty("c6", "false")));

                    gui.radioButton1.setSelected(Boolean.valueOf(prop.getProperty("r1", "false")));
                    gui.radioButton2.setSelected(Boolean.valueOf(prop.getProperty("r2", "false")));
                    gui.radioButton3.setSelected(Boolean.valueOf(prop.getProperty("r3", "false")));
                    gui.radioButton4.setSelected(Boolean.valueOf(prop.getProperty("r4", "false")));
                    gui.radioButton5.setSelected(Boolean.valueOf(prop.getProperty("r5", "false")));
                    gui.radioButton6.setSelected(Boolean.valueOf(prop.getProperty("r6", "false")));
                    gui.radioButton7.setSelected(Boolean.valueOf(prop.getProperty("r7", "false")));
                    gui.radioButton8.setSelected(Boolean.valueOf(prop.getProperty("r8", "false")));
                    gui.radioButton9.setSelected(Boolean.valueOf(prop.getProperty("r9", "false")));
                    gui.radioButton10.setSelected(Boolean.valueOf(prop.getProperty("r10", "false")));
                    gui.radioButton11.setSelected(Boolean.valueOf(prop.getProperty("r11", "false")));
                    gui.radioButton12.setSelected(Boolean.valueOf(prop.getProperty("r12", "false")));

                    gui.radioButton14.setSelected(Boolean.valueOf(prop.getProperty("r14", "false")));
                    gui.radioButton15.setSelected(Boolean.valueOf(prop.getProperty("r15", "false")));
                    gui.radioButton16.setSelected(Boolean.valueOf(prop.getProperty("r16", "false")));
                    gui.radioButton17.setSelected(Boolean.valueOf(prop.getProperty("r17", "false")));
                    gui.radioButton18.setSelected(Boolean.valueOf(prop.getProperty("r18", "false")));
                    gui.radioButton19.setSelected(Boolean.valueOf(prop.getProperty("r19", "false")));
                    gui.radioButton20.setSelected(Boolean.valueOf(prop.getProperty("r20", "false")));
                    gui.radioButton21.setSelected(Boolean.valueOf(prop.getProperty("r21", "false")));
                }
            } catch (Throwable e) {}
        }
    }
Settings prop = new Settings(new File(System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "Data" + File.separator), this, script.myPlayer().getName());

gives me an onStart error -.- smh

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...