Store JComboBox Selected Item using Serialization

Till now, we've seen related to Swing on Serialization.

1. Storing JFrame Size,Location using Serialization.
2. Updation in Serialization, Storing JButton clicks.
3. Using Serialization on JCheckBox.
4. Using Serialization on JToggleButton

Now, let us see storing a JComboBox selected item. Not just this, the following program comes with a little piece of code that you might be searching for, in fact that i to like too, that's nothing but disabling a button upon click and enabling it after the user changes the input. If you can observe on Facebook, when you type your username and password, hit enter the Login button gets disabled effect. That's what i like it and this type of effect is also mostly observed.



Here is the program


Selected Item for JComboBox Stored.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class SerializeJComboBox extends JFrame
{
JComboBox jc;
JButton jb;
public SerializeJComboBox()
{
setTitle("JComboBox Example");
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

jc=new JComboBox();

jc.addItem("Apples");
jc.addItem("Oranges");
jc.addItem("Mangoes");
jc.addItem("Watermelon");

add(jc);

try
{
FileInputStream fin=new FileInputStream("si.dat");
ObjectInputStream oin=new ObjectInputStream(fin);
JCData c=(JCData)oin.readObject();
jc.setSelectedItem(c.selectedItem);

oin.close();
fin.close();
}catch(Exception e){}

jc.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
jb.setEnabled(true);
}
});

jb=new JButton("Set");

jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
try
{
FileOutputStream fout=new FileOutputStream("si.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);

JCData c=new JCData();
c.selectedItem=(String)jc.getSelectedItem();

out.writeObject(c);

out.close();
fout.close();

jb.setEnabled(false);
}catch(Exception e){}
}
});
add(jb);

setExtendedState(MAXIMIZED_BOTH);
}

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

class JCData implements Serializable
{
String selectedItem;
}

}


Explanation


All the GUI Stuff is common for every program. If you want to learn more then see my post on Using JComboBox

fin=new FileInputStream("si.dat"): Create a FileInputStream object pointing si.dat

oin=new ObjectInputStream(fin): Create an ObjectInputStream object pointing fin

c=(JCData)oin.readObject(): This method reads object from the file si.dat and then returns it in the form of java.lang.Object which is typecasted into JCData

jc.setSelectedItem(c.selectedItem): This method takes java.lang.Object as parameter and selects that object in JComboBox. The previously stored value of selectedItem in JCData is selected. If nothing is stored (i.e. if the program is first time executed, [then there will not be si.dat]), this is not reached because the exception is raised in the above steps itself and the Exception object is thrown to the corresponding catch block.

jb.setEnabled(true): Make the button jb enabled, so that some action can be performed on it. It is disabled when user selects an item and clicks on it (or perform an action) and now we are making it enabled when item's state is changed i.e. if some other item is selected.

fout=new FileOutputStream("si.dat"): Create FileOutputStream object pointing si.dat

out=new ObjectOutputStream(fout): Create ObjectOutputStream object pointing fout

c.selectedItem=(String)jc.getSelectedItem(): Change the value of selectedItem in JCData with the value of selected item in JComboBox. The getSelectedItem() method returns java.lang.Object so we have typecasted it into java.lang.String

setExtendedState(MAXIMIZED_BOTH): After all the components are added make the frame maximized.

No comments: