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

  • SEARCH
  • Home
  • 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 6915471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:28:45+00:00 2026-05-27T09:28:45+00:00

I’m trying to serialize all of my objects when the user clicks on the

  • 0

I’m trying to serialize all of my objects when the user clicks on the File menu save command.
If I’m understanding correctly, the defaultReadObject() and defaultWriteObject() saves all of my objects except from static members so I tried to use these. My program is a kind of graph drawing program. It has a control frame where the user can set up the algorithm to run, and an other frame where the graph is painted.The the user can click on a next and previous button to see which nodes the algorithm is going through. So I’d like to save the actual state of the drawing so the user can continue the stepping after exit.
I’ve read that the defaultWriteObject should be called from the writeObject method but my problem is I don´t know how can I save the instance of the Control and Draw2 class from the actionlistener, which are created in the main function?
The relevant part of my code:

public class Control extends JPanel implements Serializable{

private String sourceFolder;
private String selectedAlg;
public List<Node> nodeList;
private JMenuBar jMenuBar;
private JMenu jMenu;
private JMenuItem jMenuItem;
private Draw2 draw;
private Search s;
//...
//and a lot more variables

void createControl(JFrame f, Draw2 d){
    jMenuBar = new JMenuBar();
    jMenu = new JMenu();
    jMenuItem = new JMenuItem();

    jMenu.setText("File");
    jMenuBar.add(jMenu);

    jMenuItem.setText("Save");
    jMenuItem.addActionListener(new SaveAction());

    jMenu.add(jMenuItem);

    //building the control window with textfields, buttons..

    draw=d;
    s=new Search();
}

private void writeObject( ObjectOutputStream out) throws IOException {

      out.defaultWriteObject();
}

//inner class, actionlistener for save menuItem
public class SaveAction implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {

        try {
            FileOutputStream f = new FileOutputStream("savedfile");
            ObjectOutputStream out = new ObjectOutputStream(f);

            //So how can save the instance of control and draw class
 //here, which is created in the main function? 
//how to call writeObject and with what parameter?

        } catch (IOException o) {   
        }
    }
}

public static void main(String[] args){

    JFrame frame = new JFrame("Control");
    JFrame frame2 = new JFrame("Draw");

    Control cont = new control();
    Draw2 gp = new Draw2(cont);

    cont.createControl(frame, gp);

    frame.setJMenuBar(cont.jMenuBar);
    gp.setPreferredSize(new Dimension(0,0));
    //Control Frame
    frame.setSize(800,330);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(cont, BorderLayout.NORTH);
    frame.setVisible(true);
    //Drawing Frame
    frame2.setSize(800,600);
    frame2.setLocation(0, 330);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    frame2.add(gp);
    frame2.setVisible(true);        

}

}

public class Draw2 extends JPanel implements Serializable{

     public boolean toDraw = false;
     public boolean timedDraw = false;
     public boolean steppedDraw = false;
     private control c;

    public Draw2(control co){
        c=co;
    }
    public void paintComponent (Graphics g) {   
    //drawing the graph
    }
}

And one last question. When I load the object from the saved file how can I replace the running object to the loaded object? I thought about doing it through a paramater but actionPerformed cannot have a parameter besides ActionEvent.

 public class LoadAction implements ActionListener{

       Control contr;

      public LoadAction(Control control) {
           contr = control;
      }

    public void actionPerformed(ActionEvent arg0) {
        try {
            FileInputStream f = new FileInputStream("savedfile");
            ObjectInputStream in = new ObjectInputStream(f);
            contr= (Control)in.readObject();
           //so now I have the loaded object in the contr object, 
           //but How can copy it over the current object?
            in.close();

            System.out.println(contr.aktIndex);
        } catch (IOException o) {
            o.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

  • 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-27T09:28:45+00:00Added an answer on May 27, 2026 at 9:28 am

    Extend action listener and pass the handle of the object to be saved to this action listener in the constructor.

    You could move the object to be saved a class variable to do this.

    public class SaveAction extends ActionListener {
    
      Control control;
    
      public SaveAction(Control control) {
        this.control = control;
      }
    
      public void actionPerformed(ActionEvent e) {
        // your save code here
      }
    
    }
    

    and then pass control when you new SaveAction in your code above…

    new SaveAction(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.