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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:29:13+00:00 2026-05-23T01:29:13+00:00

I have these lines of code. I know you can not pass a non

  • 0

I have these lines of code. I know you can not pass a non final variable to an inner class but I need to pass the variable i to the anonymous inner class to be used as a seatingID. Can you suggest ways of doing that ?

JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
    seats[i] = new JButton();//creating the buttons
    seats[i].setPreferredSize(new Dimension(50,25));//button width
    panel4seating.add(seats[i]);//adding the buttons to the panels

    seats[i].addActionListener(new ActionListener()
    {  //anonymous inner class
        public void actionPerformed(ActionEvent evt)
        {  
            String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
            String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");

            sw101.AddPassenger(firstName, lastName, seatingID);
        }
    });
}
  • 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-23T01:29:13+00:00Added an answer on May 23, 2026 at 1:29 am

    The simple way is to create a local final variable and initialize it with the value of the loop variable; e.g.

        JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
        for (int i = 0; i < 40; i++)
        {
            seats[i] = new JButton();//creating the buttons
            seats[i].setPreferredSize(new Dimension(50,25));//button width
            panel4seating.add(seats[i]);//adding the buttons to the panels
            final int ii = i;  // Create a local final variable ...
            seats[i].addActionListener(new ActionListener()
             {  //anonymous inner class
                public void actionPerformed(ActionEvent evt)
                {  
                    String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
                    String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
    
                    sw101.AddPassenger(firstName, lastName, ii);
                }
             });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to decouple code you can have service locater's but is this not
How can I have each of these a elements break on to new lines,
I have these lines in my ~/.inputrc : set editing-mode vi set keymap vi
I have these (sample) lines in a HTML-file: test.ABC.test test.ABCD.test test.ABCE.test And this Ant
I have defined a custom file type with these lines: syn region SubSubtitle start=+=+
I have a txt file that created in notepad , containing these lines for
I am little confused with logical AND operator. I have these 2 lines of
I have the following code in Python: def point_to_index(point): if point not in points:
I have this code, what It returns is a list of some clients, but
NOTICE: Appericiate all the answers, thanks but we already have a sequence.. and can't

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.