saves


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: saves

  1. #1
    ALEX Guest

    saves


    If I want to create a button that When I click it , it will saves any setting
    thatI had made. For example, lets say that i create a radiobutton and I
    put a check on it and what I want is that when I exit the application and
    I comeback it will retain any modification that I did previuosly.

  2. #2
    Shayne Guest

    Re: saves


    Here's one way (maybe not the best though
    I am going to show you two classes:

    /******************************************************/
    // This is the class that you will use to save the
    // settings that you have made.
    // cBox1 is true only if the CheckBox in
    // your application should be checked.
    // The fact that this class implements Serializable
    // allows us to save an instance of itself to a file.

    public class Settings implements Serializable
    {
    boolean cBox1;

    public Settings()
    {
    cBox1 = false;
    }
    }
    /******************************************************/

    and

    /******************************************************/
    // This is the program that you are writing.
    // I am going to leave out all of the GUI stuff,
    // assuming that you know how to do that.

    public class MyProgram
    {
    Settings settings;


    // Your window can take any constructors you want, but
    // adding the Settings constructor allows you to set
    // the GUI components to their saved states.
    public MyProgram(Settings s)
    {
    settings = s;

    // initialize all GUI components
    // including CheckBox checkBox1
    // (I am assuming AWT, but these ideas
    // work exactly the same in Swing)

    if(s.cBox1)
    {
    checkBox1.setState(true);
    }
    else
    {
    checkBox1.setState(false);
    }

    // add an actionListener (which I will define below)
    // to checkBox1
    checkBox1.addActionListener(new MyActionListener());

    // add a WindowEventListener (which I will define below)
    // to save settings when the user exits
    this.addWindowListener(new MyWindowListener());
    }

    // This is an inner class.
    // It is an ActionListener which updates settings when the
    // checkBox changes state.
    class MyActionListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    settings.cBox1 = checkBox1.getState();
    }
    }

    // This is an inner class.
    // It is a WindowListener which saves settings when the
    // user exits.
    class MyWindowListener extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    // To save settings, we create a FileOutputStream
    // and save the settings object to "settings.data"
    // We can do this because settings implements
    // Serializable

    try
    {
    FileOutputStream fos = new FileOutputStream ("settings.data");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(settings);

    // to be good little programmers,
    // we should close our streams
    oos.close();
    fos.close();

    }
    catch(Exception e)
    {}
    }
    }

    // This is the entry point for your program.
    // It will look for a file called "settings.data"
    // If it finds it, it will initialize your Window with the
    // object
    // stored in "settings.data"
    // If it doesn't find it, it will create a new Settings
    // instance to
    // initialize your Window with.
    public static void main(String[] args)
    {
    Settings sets;

    try
    {
    FileInputStream fis = new FileInputStream("settings.data");
    ObjectInputStream ois = new ObjectInputStream(fis);

    // make sure to cast here
    sets = (Settings) ois.readObject();
    }
    catch(Exception e)
    {
    // If we can't find the file, then no settings
    // have yet been saved, so initialize a new
    // instance of Settings.
    sets = new Settings();
    }

    // Open your program window with the sets constructor
    MyProgram window = new MyProgram(sets);
    }
    }
    /******************************************************/

    I hope that this makes some degree of sense.
    Feel free to email me with questions: soneill@mindspring.com



    "ALEX " <cad1374@yahoo.com> wrote:
    >
    >If I want to create a button that When I click it , it will saves any setting
    >thatI had made. For example, lets say that i create a radiobutton and I


    >put a check on it and what I want is that when I exit the application

    and
    >I comeback it will retain any modification that I did previuosly.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center