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


Serializable CheckBox


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class SerializeCheckBox extends JFrame implements ItemListener
{
JCheckBox jc1,jc2,jc3,jc4;
public SerializeCheckBox()
{
setTitle("Checkbox Serialized.");
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);


jc1=new JCheckBox("Apples");
jc2=new JCheckBox("Mangoes");
jc3=new JCheckBox("Oranges");
jc4=new JCheckBox("Bananas");

try
{
FileInputStream fin=new FileInputStream("ch.dat");
ObjectInputStream oin=new ObjectInputStream(fin);

ChData ch=(ChData)oin.readObject();
jc1.setSelected(ch.apples);
jc2.setSelected(ch.mangoes);
jc3.setSelected(ch.oranges);
jc4.setSelected(ch.bananas);
}catch(Exception e){
// Exception is throwed for first time since file is not found.
}

jc1.addItemListener(this);
jc2.addItemListener(this);
jc3.addItemListener(this);
jc4.addItemListener(this);

add(jc1);
add(jc2);
add(jc3);
add(jc4);
}
public void itemStateChanged(ItemEvent ie)
{

                 try
                    {
FileOutputStream fout=new FileOutputStream("ch.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);

ChData ch=new ChData();
ch.apples=jc1.isSelected();
ch.mangoes=jc2.isSelected();
ch.oranges=jc3.isSelected();
ch.bananas=jc4.isSelected();

out.writeObject(ch);
out.close();
fout.close();
                   }catch(Exception e){}

}
public static void main(String args[])
{
new SerializeCheckBox();
}

                        class ChData implements Serializable
                        {
                        boolean apples,mangoes,oranges,bananas;
                        }

}

Explanation


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