Store JFrame Size, Location using Serialization

Store JFrame Size, Location. How do software do and how we can in Java?



Have you ever thought of, how software windows/frames be in the same position and of same size as they were previously when you open them the next time. We can do it in Java and that's simple too, they store the location, size of the frame when user changes it. The software store the data either in Windows Registry or in the form of a file. In the following example you will see how to store the size and location of JFrame using Serialization.

The program looks long, but understanding it is simple. Have a look

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class SerializeJFrame extends JFrame
{
public SerializeJFrame()
{
setTitle("Size Serialized.");
setDefaultCloseOperation(EXIT_ON_CLOSE);

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

JFData jf=(JFData)oin.readObject();
setLocation(jf.x,jf.y);
setSize(jf.size);

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

setVisible(true);

addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent ce)
{

try
{

FileOutputStream fout=new FileOutputStream("jf.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);

JFData jf=new JFData();
jf.x=getLocation().x;
jf.y=getLocation().y;
jf.size=getSize();

out.writeObject(jf);

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

}catch(Exception e){}

}
public void componentMoved(ComponentEvent ce)
{

try
{

FileOutputStream fout=new FileOutputStream("jf.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);

JFData jf=new JFData();

jf.x=getLocation().x;
jf.y=getLocation().y;
jf.size=getSize();

out.writeObject(jf);

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

}catch(Exception e){}

}

});
}

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

class JFData implements Serializable
{
Dimension size=new Dimension(400,400);
int x=0,y=0;
}

}


Explanation


fin=new FileInputStream("jf.dat"): Open a file, jf.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 jf.dat

JFData ch=(JFData)oin.readObject(): Read object from the file jf.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.

setLocation(jf.x,jf.y): Get x,y values from the object stored in jf.dat and set location for JFrame

setSize(jf.size): This method takes Dimension object and the Dimension class's constructor takes width, height respectively.

Why Exception? As said, earlier for the first time, the program gets executed, FileNotFoundException because the file was not created by that time. Note Serializable exception might also be thrown, if object jf is not serialized object, however that will not arise here, because the class was serialized.

setVisible(true): To make the frame visible, you may already know it, but this is written to say why this is written here and not along with other set methods. The reason, is when this is called at first, first the default settings (size, location) of the JFrame are set and then ObjectInputStream.. is activated and then the effect will be (i.e. the user's settings [user's size,location] are set), so in order to make the user's settings much more better just before the frame is visible makes sense. If you can't understand it, you will understand it practically, don't worry try put this method along with other set methods (i.e. at the top) and then try keeping it at the position of what i did in the example. 

addComponentListener(new ComponentAdapter()): This method registers the JFrame with ComponentListener so that the ComponentListener listens when ComponentEvent is generated. If we write, ComponentListener here, instead of ComponentAdapter then, i'll need to provide body for each and every method, as i've used only two of them, i've written ComponentAdapter

componentMoved(ComponentEvent ce): Method that contains, the things to do, when JFrame is moved.

componentResized(ComponentEvent ce): Method that contains, the things to do, when JFrame is resized (i.e. it's size is changed).

fout=new FileOutputStream("jf.dat"): FileOutputStream points to jf.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 jf.dat

jf=new JFData(): jf is the object for JFData which is an inner class containing Dimension object, and int variables x,y which represent the x and y positions of the JFrame

jf.x=getLocation().x & getLocation().y: getLocation() is a method present in JFrame class which returns java.awt.Point and this class contains a data member 'x','y' which contains values of x,y co ordinates of JFrame respectively and the values are stored in x,y in jf respectively. getLocation() is from java.awt.Component.

jf.size=getSize(): Returns size of JFrame, present in java.awt.Component class. The size is stored in size variable which is of Dimension, the type that getSize() returns. By default in the class, size is declared to be of width 400 and height 400.

out.writeObject(jf): Write the data that you have stored in the object jf into jf.dat

Also see Understanding Java Serialization for better understand if you don't have touch with Serialization


Posted by at on

Tags: Serialization on Swing,