Using Serialization on JCheckBox
JCheckBox and Serializable
Let's be in real now, i want to store the state of JCheckBox (whether it is selected or not) just as some software store the data in a file. As said earlier in Understanding Java Serialization, we'll combine Serialization and the JCheckBox as i've got it in my product Booster+
JCheckBox & Serializable Example
class SerializeCheckBox extends JFrame implements ItemListener
FileInputStream fin=new FileInputStream("ch.dat");
ObjectInputStream oin=new ObjectInputStream(fin);
// Exception is throwed for first time since file is not found.
public void itemStateChanged(ItemEvent ie)
FileOutputStream fout=new FileOutputStream("ch.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);
ChData ch=new ChData();
public static void main(String args)
class ChData implements Serializable
I think, you might have understood till, creating JCheckBoxes using the constructors, and also hope adding the JCheckBoxes. Fine. Let's talk about the main theme.
fin=new FileInputStream("ch.dat"): Open a file, ch.dat for reading, you'll have to note that, when the program was executed for the first time, this file is not found, because FileOutputStream is called later.
oin=new ObjectInputStream(fin): Create ObjectInputStream pointing to the FileInputStream which further points to ch.dat
ChData ch=(ChData)oin.readObject(): Read object from the file ch.dat. The method readObject() returns java.lang.Object so, we need to type cast into the type of the object which was written in the file.
setSelected(): This method takes, boolean value i.e true when if you want the JCheckBox to be selected and false when you want to make it unchecked.
Why Exception? As said, earlier for the first time, the program gets executed, FileNotFoundException because the file was not created by that time. Not Serializable exception might also be thrown, if object ch is not serialized object, however that will not arise here, because the class was serialized.
itemStateChanged(ItemEvent ie): Method that contains, the things to do, when any one of the JCheckBox's state is changed. You can alternatively write anonymous inner class, that will be good but lengthy if you want to write for each and every JCheckBox you've written.
fout=new FileOutputStream("ch.dat"): FileOutputStream points to ch.dat which will be created for the first time and will be overridden for the next times.
out=new ObjectOutputStream(fout): out is the object of ObjectOutputStream which points to fout which is the Object for FileOutputStream and this object points to ch.dat
ch=new ChData(): ch is the object for ChData which is an inner class containing boolean values. It is your wish, you can write it as inner class or outside. By default, all the boolean values are false
isSelected(): This method, belonging to javax.swing.JCheckBox returns a boolean value which is the state of the JCheckBox i.e. true is returned when JCheckBox is selected and false when JCheckBox is not selected.
out.writeObject(ch): Write the data that you have stored in the object ch to the file ch.dat