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 6900665
In Process

The Archive Base Latest Questions

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

I am trying to learn more about java. This program is an attempt to

  • 0

I am trying to learn more about java. This program is an attempt to understand events as well as serialization. What i am attempting to do is flatten an object when the user closes the JFrame and re-inflate it when the program is started. I know i can create the serialized file but having it take effect again isn’t working. Any help in the right direction would be wonderful. Thank you in advance.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt"; 
public int ToL = 0;
String outT = Integer.toString(ToL);
    JLabel jl = new JLabel(outT);

FileOutputStream fos = null;
ObjectOutputStream out = null;

public tempusFugit()

{
    Timer timer = new Timer(1000, this);

    setBounds(250, 250, 250, 190);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT) );
    setVisible(true);
    add(jl);
    timer.start();
}

public void actionPerformed(ActionEvent e)
{
    ++ToL;
    outT = Integer.toString(ToL);
    jl.setText(outT);
    validate();
    repaint();
}
public static void main(String[] args)
{
    tempusFugit tf = new tempusFugit();
    tf.addWindowListener( tf );

}
public void windowDeactivated(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{   
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
    try
    {
        fos = new FileOutputStream(fileN);
        out = new ObjectOutputStream(fos);
        out.writeObject(this);
        out.close();

    }
    catch(IOException ex)
    {
}
}
public void windowOpened(WindowEvent e)
{
try
    {
        tempusFugit tf = new tempusFugit();
        FileInputStream fis = new FileInputStream(fileN);
        ObjectInputStream in = new ObjectInputStream(fis);
        tf = (tempusFugit)in.readObject();
        this.ToL = tf.ToL;
    }
    catch(IOException ex)
    {
    }
    catch(ClassNotFoundException ce)
    {
    }

}

}

I assume i’m trying to recreate the object at the wrong time. Even though the object is serialized correctly i can not access it again with the windowOpened function. Do i need to try to use

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

somehow?

What i end up with is an error saying i am trying to access a Final object (i assume my this). I find that very odd that i cant repopulate my current ‘this’ with another similar object.

Am i way off base?

Again thank you for your time.

  • 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-27T07:34:29+00:00Added an answer on May 27, 2026 at 7:34 am

    Note that there are much simpler ways to do this than serializing the frame, and much better things to serialize than the frame itself.

    See What is the best practice for setting JFrame locations in Java? for an example of storing the location and size of a frame. It would be trivial to adapt that to store the count.


    But here is an attempt based on your code.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.swing.Timer;
    import java.io.*;
    import java.io.Serializable;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
    {
        String fileN = "tf.txt";
        public int ToL = 0;
        JLabel jl = new JLabel("" + ToL);
    
        public tempusFugit()
        {
            Timer timer = new Timer(1000, this);
    
            setBounds(250, 250, 250, 190);
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLayout(new FlowLayout(FlowLayout.LEFT) );
            setVisible(true);
            add(jl);
            timer.start();
        }
    
        public void actionPerformed(ActionEvent e)
        {
            ++ToL;
            jl.setText("" + ToL);
            validate();
            repaint();
        }
    
        public static void main(String[] args)
        {
            tempusFugit tf = new tempusFugit();
            tf.addWindowListener( tf );
        }
    
        public void windowDeactivated(WindowEvent e){}
        public void windowActivated(WindowEvent e){}
        public void windowDeiconified(WindowEvent e){}
        public void windowIconified(WindowEvent e){}
        public void windowClosed(WindowEvent e){}
    
        public void windowClosing(WindowEvent e)
        {
            try
            {
                FileOutputStream fos = new FileOutputStream(fileN);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject(this);
                out.flush();
                out.close();
                setVisible(false);
                System.exit(0);
            }
            catch(IOException ex)
            {
                JOptionPane.showMessageDialog(null, ex);
                System.exit(1);
            }
        }
    
        public void windowOpened(WindowEvent e)
        {
            try
            {
                tempusFugit tf;// = new tempusFugit();
                FileInputStream fis = new FileInputStream(fileN);
                ObjectInputStream in = new ObjectInputStream(fis);
                tf = (tempusFugit)in.readObject();
                this.ToL = tf.ToL;
                //tf.setVisible(false);
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
            catch(ClassNotFoundException ce)
            {
                ce.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to learn more about lambda expressions. I read this question on
I've been lately trying to learn more and generally test Java's serialization for both
Right now i am trying to learn more about java threading, and i have
I'm trying to write some code in java to learn more about coding with
I'm currently trying to learn more about object oriented design in C++ (familiar with
I was trying to learn more about bits, and I came across this example
I'm trying to learn more about basic Java and the different types of Throwables,
I've been trying to learn more about access modifiers in java, and everyone has
I have been trying to learn more about lambda expressions lately, and thought of
I am trying to learn more about regular expressions I have one below that

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.