5

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);
        }
    });
}
HoLyVieR
  • 10,985
  • 5
  • 42
  • 67
dave
  • 419
  • 1
  • 5
  • 10
  • You'll get better answers if you present the minimal bit of compilable code that demonstrates the bug. Do you mean 'i' where you have 'seatingID' in the above code? – Burleigh Bear Jun 12 '11 at 03:04
  • there is no bug actually, Im trying to figure out a way to pass the variable i from the for loop to the inner class so i can assign it as a seatingID – dave Jun 12 '11 at 03:07

2 Answers2

8

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);
            }
         });
    }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

You can't directly, but you can make a (static private) subclass of ActionListener that takes a seatingID in its constructor.

Then rather than

seats[i].addActionListener(new ActionListener() { ... });

you'd have

seats[i].addActionListener(new MySpecialActionListener(i));

[Edit] Actually, there's so much else wrong with your code that I'm not really sure that this advice is good. How about presenting code that would compile.

Burleigh Bear
  • 3,274
  • 22
  • 32
  • which part? I am new to swing. this is just part of the program. – dave Jun 12 '11 at 03:03
  • sw101 is not declared anywhere, seatingID is not declared anywhere, what happens when the user hits the button more than once? See my comment to the main question. – Burleigh Bear Jun 12 '11 at 03:05
  • sw101 is a flight object and it is declared somewhere, my problem was to figure out a way to pass the variable i. – dave Jun 12 '11 at 03:08