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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:15+00:00 2026-05-26T22:56:15+00:00

I am trying to program a java application that consists of several windows using

  • 0

I am trying to program a java application that consists of several windows using JFrame.

Each JFrame contains a JTextField and buton to go to the next JFrame.

I need to retrieve all the information entered by the user at the end.

I created an event click on the buton to save to a public class all the data that the user introduce in the JTextField. I named that public class myData, which has a static attributes.

The problem is that I can not access this class from the button listener function.

I get an error: cannot refer to an non final variable inside an inner class defined in a different method.

My goal is to be able to share the class myData between different methods of a different class.

E.g. I have a class named myClass1 and myClass2, so I want to share the myData attribute between myClass1 methods and myClass2 methods.

Please anyone can someone help me? or propose another way to do this!

Thanks in advance !

  • 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-26T22:56:15+00:00Added an answer on May 26, 2026 at 10:56 pm

    All of the calls about MVC etc. are valid, but this isn’t that hard.

    What you want to do is in your Main, you can create your Data (Model) class, the class that holds all of your information.

    So, you can do something like this:

    public class F1 ... {
        private final Data myData;
    
        public F1(Data theData) {
            myData = theData;
        }
    
        ....
    }
    
    public class Main {
        Data myData;
    
        public static void main(String args[]) {
            Main m = new Main();
            m.setMyData(new Data());
    
            F1 f = new F1(m.getMyData());
            ...
         }
    }
    

    Then, later, when F1 calls F2, simply do the same thing — create F2 with the Data passed in earlier by the constructor. That way, as each Frame runs its course, they’re all working on the same instance of Data. When all is done, the single instance of Data is left within the Main class for you to do with what you will.

    There are better ways to reorganize your entire program, but this should give you ideas on how to get over the hump you’re having right now.

    Addenda:

    There are several things you can do.

    When your get the ActionEvent, it contains a source. That source is the component that generated the event (most likely a Button in this case). If you know where the button is located in the hierarchy of things, you get to your Frame directly. In the pastebin example, you have Frame -> Panel -> Button. So, if you have the Button, you cat get to the Frame.

    public void actionPerformed(ActionEvent e) {
        JButton sourceButton = (JButton)e.getSource();
        F1 f1 = (F1)sourceButton.getParent().getParent();
        Data myData = f1.getMyData();
        data.setField(...);
    }
    

    Again, this is not the recommended ways of doing things. The tutorials have decent examples of using MVC and property change listeners and the whole kit. But this should get you to where you want to go.

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

Sidebar

Related Questions

I am trying to write a Java program that reads an input file consisting
I'm trying to write a program in Java that uses osql to generate a
For my java program I'm using Rhino to execute JS scripts. Now I'm trying
I am trying to make a program in Java that checks for three specific
I'm trying to follow Java's JDBC tutorials to write a Java program that can
I am new to Java and am trying to run a program using Eclipse.
I've been trying to use Java's ProcessBuilder to launch an application in Linux that
I'm running a java application that we distribute as a server-side system. I'm trying
I've got a Java application that I'm writing an installer for. We're using the
I'm trying to send keystrokes from my C# program to a Java application The

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.