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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:28:15+00:00 2026-06-16T00:28:15+00:00

Ok, so I couldn’t find exactly what I needed anywhere else, so here goes.

  • 0

Ok, so I couldn’t find exactly what I needed anywhere else, so here goes.

How would I go about passing a variable into a function inside an ActionListener()? This is my code.

for(int y = 0; y < 5; y ++) {
        for(int x = 0; x < 5; x ++) {
            currentRect = (y * 5) + x;
            mainButtons.get(currentRect).addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    answerField.setText(questions.get(currentRect).getAnswer());
                }
            });
        }
    }

The error is on line six, where it underlines “currentRect”, and then complains about this,

Cannot refer to a non-final variable currentRect inside an inner class defined in a different method

What is does is iterate through a group of JButtons (javax.swing) , and then assign the appropriate action listeners based on their position in the ArrayList, however, I can’t seem to pass the variable storing their positions into the actionlistener itself, so I’m stuck. I realised while im writing this that I should really use a foreach, so ill change that later. (just a little note to who was gonna point that out) I realize that I cannot also make that variable final, as it is changed during the for loop. I also tried passing the value of the currentRect ((y * 5) + x) into the functions, but to no avail.

Thanks,

andrewgies17

  • 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-06-16T00:28:16+00:00Added an answer on June 16, 2026 at 12:28 am

    The solution is simple, simply declare a new final variable inside of your block:

    for(int y = 0; y < 5; y ++) {
            for(int x = 0; x < 5; x ++) {
                final int currentRect = (y * 5) + x;
    

    or if you need the value of currentRect outside of the block:

    for(int y = 0; y < 5; y ++) {
            for(int x = 0; x < 5; x ++) {
                currentRect = (y * 5) + x;
                final int currentRect2 = currentRect;
    

    and you use currentRect2 instead of currentRect in your anonymous class. This is counterintuitive because it looks like that the final variable is beeing re-initialised each time but in fact, you must see this as a new variable that is initialised only once for each loop of the block.

    However, this is not the most intelligent way of solving this problem; because you are creating a brand new anonymous class for each button just for the purpose of storing a small value that is different between them. These can quickly add up on a limited device. A better idea would be to store the value of the currentRect as a tag for each button. This way, you can declare a single instance of the anonymous class in the outer class and reuse it for all buttons.

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

Sidebar

Related Questions

I couldn't find anything about getting the total JSON record count using jQuery. Here
I couldn't find anything similar to this anywhere. I have an array of pointers
Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to
I couldn't find any information about how to dump a MySQL database with an
I couldn't find any documentation on gitk about what the colors mean, but the
Couldn't find a function for this. I'm assuming I need to use regex? I'm
I couldn't find or come up with a generic and elegant algorithm that would
I couldn't find any question similar to this one, I wanted to know about
Couldn't find any documentation about this: what is the order of execution of the
couldn't find similar posts, so posting my own question. I got variable array of

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.