Results 1 to 8 of 8
  1. #1
    usha is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default How to save a panel object

    Hi,
    I am trying to save Panel object of JFrame.I used serialization concept for this.I implemented the code as

    ObjectOutputStream oos=null;
    FileOutputStream fos = new FileOutputStream("D:\\ok");
    oos = new ObjectOutputStream(fos);
    oos.writeObject(jPanel1);



    But iam getting "GroupLayout Not Serializable" exception .I am using Netbeans IDE so it takes GroupLayout as default and generates code.How to make GroupLayout class Serializable.please help me.

    Thanks in advance,
    usha.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not familiar with GroupLayout, but this smells like a bad idea to me. Wouldn't you rather save the program data's state rather than the whole GUI itself? Besides, all Swing classes come with a warning that serialization may not be supported in the future. What's the reason behind your wanting to do this?

  3. #3
    usha is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    Thank you for your reply.

    I have added some components on that panel. Now when i click save button i want save that whole object with those components also. when i used other Layout instead of GroupLayout my panel object saved successfully. But if i used GroupLayout i am getting "GrouoLayout not Serializable Exception". I designed my total application with GroupLayout. Now its impossible to me to change total application to other Layout. please help me.

    Thanks in advance,
    Usha.

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Even I am not familiar with GroupLayout, and the solution suggested above looks better than mine. However the following might be one solution although untested.

    Java Code:
    private  void serialize(){
    	try{
    		//just before serialization change the layout to something else
                    panel.setLayout(new FlowLayout());
    		FileOutputStream fos=new FileOutputStream("c:\\a1.obj");
    		ObjectOutputStream oo=new ObjectOutputStream(fos);
    		oo.writeObject(panel);
    		oo.close();
    		fos.close();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    }
    
    private  void deserialize(){
    	try{
    		//pnl.setLayout(new FlowLayout());
    		FileInputStream fis=new FileInputStream("c:\\a1.obj");
    		ObjectInputStream ii=new ObjectInputStream(fis);
    		panel=(JPanel)ii.readObject();
                    //after deserialization change the layout back to GroupLayout
    		GroupLayout layout = new GroupLayout(panel);
    		panel.setLayout(layout);
    		ii.close();
    		fis.close();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    }
    Swastik

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    @dswastik : bad guess. I suggest you test any 'solutions' before posting here.

    Setting a layout on a container won't automagically determine all and appropriate constraints associated with the components it holds.

    db

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Now its impossible to me to change total application to other Layout.
    What's your problem with saving the state instead, as suggested by Fubarable?

    db

  7. #7
    usha is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    Thank you for your reply.

    could you please give me any idea about how to save state of an object without using serialization.

    Thanks in advance,
    Usha

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by usha View Post
    could you please give me any idea about how to save state of an object without using serialization.
    For me it's hard to know what to suggest without more information on your part, more details on the specifics. For suggestions on what type of information we need, please see the second link in my signature links below entitled, how to ask smart questions, which has much useful information that has helped me formulate questions. I try to review that article at least once a month.

    One other thing, you could get around your serialization error by declaring the GroupLayout variable as a transient variable. This way, the serialization mechanism won't try to serialize this object. The down side is that you'll need to have custom code to de-serialize the data and re-create your GroupLayout object.
    Last edited by Fubarable; 12-11-2010 at 02:45 PM.

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 09:31 PM
  2. Is this the proper way to save an object?
    By ryuzog in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 03:32 AM
  3. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 09:43 PM
  4. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  5. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •