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.
12-05-2000, 02:03 AM
Shayne
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)
// 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.