Since both JButton and JTree have implemented the serializable interface, the serialization and reading of the Java Swing components can be done.
The method is to use ObjectInputStream to read the objects in the file and write the object to the file using ObjectOutputStream.
Such as: import java.io.FileInputStream;
import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Javax.swing.JButton;
Import Javax.swing.JTree;
public class Save {
public static void Main (string[] args) {
//Write
JButton button = new JButton ("TEST button");
JTree tree = new JTree ();
try {
ObjectOutputStream Outforbutton = new ObjectOutputStream (
New FileOutputStream ("button");
Outforbutton.writeobject (button);
Outforbutton.close ();
ObjectOutputStream outfortree = new ObjectOutputStream (
New FileOutputStream ("tree");
Outfortree.writeobject (tree);
Outfortree.close ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace (); }
//Read
try {
ObjectInputStream Inforbutton = new ObjectInputStream (
New FileInputStream ("button"));
JButton buttonreaded = (JButton) inforbutton.readobject ();
ObjectInputStream infortree = new ObjectInputStream (
New FileInputStream ("tree");
JTree treereaded = (jtree) infortree.readobject ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace (); } catch (ClassNotFoundException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
}