Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 746165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:40:03+00:00 2026-05-14T09:40:03+00:00

I have managed to serialize my very basic GUI-object containing a JTextArea and a

  • 0

I have managed to serialize my very basic GUI-object containing a JTextArea and a few buttons to a file ‘test.ser’.

Now, I would like to completely restore the previously saved state from ‘test.ser’, but seem to have a misconception of how to properly deserialize an objects state.

The class MyFrame creates the JFrame and serializes it.

public class MyFrame extends JFrame implements ActionListener {


 // Fields
 JTextArea textArea;
 String title;
 static MyFrame gui = new MyFrame();
 private static final long serialVersionUID = 1125762532137824262L;


 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  gui.run();
 }

 // parameterless default contructor
 public MyFrame() {

 }

 // constructor with title
 public MyFrame(String title) {

 }

 // creates Frame and its Layout
 public void run() {

  JFrame frame = new JFrame(title);
  JPanel panel_01 = new JPanel();
  JPanel panel_02 = new JPanel();

  JTextArea textArea = new JTextArea(20, 22);
  textArea.setLineWrap(true);

  JScrollPane scrollPane = new JScrollPane(textArea);

  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

  panel_01.add(scrollPane);



  // Buttons
  JButton saveButton = new JButton("Save");
  saveButton.addActionListener(this);
  JButton loadButton = new JButton("Load");
  loadButton.addActionListener(this);


  panel_02.add(loadButton);
  panel_02.add(saveButton);
  // Layout
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(BorderLayout.CENTER, panel_01);
  frame.getContentPane().add(BorderLayout.SOUTH, panel_02);

  frame.setSize(300, 400);
  frame.setVisible(true);
 }

 /*
  * 
  */
 public void serialize() {

  try {
   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.ser"));
   oos.writeObject(gui);
   oos.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }


 public void actionPerformed(ActionEvent ev) {
  System.out.println("Action received!");
  gui.serialize();
 }

}

Here I try to do the deserialization:

public class Deserialize {
 static Deserialize ds;
 static MyFrame frame;



 public static void main(String[] args) {
  try {
   ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser"));
    frame = (MyFrame) ois.readObject();
    ois.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

Maybe somebody could point me into the direction where my misconception is?

How would you guys write a class, which deserializes and restores the previously serialized gui-elements to their previously serialized state?

The way I am doing it right now seems to have more than one flaw in its concept, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:40:04+00:00Added an answer on May 14, 2026 at 9:40 am

    What happens? Are you getting an exception? From the looks of the code ds is never initialised. I believe, once deserialised, you will need to show the frame with frame.setVisible(true);. As always, Swing (and in fact AWT) should be used only on the AWT Event Dispatch Thread (EDT) – use java.awt.EventQueue.invokeLater.

    Generally using statics is not a good idea. Nor is serialising GUI components. final is good, and will, for the most part, make sure instance and static fields are initialised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What exactly is happening in your OS's TCP/IP stack (possibly… May 15, 2026 at 8:28 pm
  • Editorial Team
    Editorial Team added an answer There is no need to use inline views and I… May 15, 2026 at 8:28 pm
  • Editorial Team
    Editorial Team added an answer if you want : independent couters for each thread, you… May 15, 2026 at 8:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.