0

I am trying to add an anonymous actionListener to a JCheckBox but having some difficulty in accessing the object I want to update the value with. I keep getting errors about non final, and then when i change them to be final it complains about other things.
what im trying to do is below (i've removed some of the gui code to make it easier to read):

for (FunctionDataObject fdo : wdo.getFunctionDataList())
{
    JLabel inputTypesLabel = new JLabel("Input Types: ");
    inputsBox.add(inputTypesLabel);
    for (int i = 0; i < fdo.getNumberOfInputs(); i++)
    {
        JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
        JComboBox inputTypeComboBox = new JComboBox(getTypes());
        inputTypeComboBox.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) 
             {
                 fdo.getInputTypes().set(i, (String) inputTypeComboBox.getSelectedItem());
             }
        });
     }
}    
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1584120
  • 1,169
  • 2
  • 23
  • 44
  • I think that issue is hidded in the code that isn't presented, for better help sooner post an [SSCCE](http://sscce.org/), there could be mistakes in code concept, not how to set final indicator for anonymous listener – mKorbel Sep 26 '12 at 09:39

3 Answers3

1

Update your code from

 inputTypeComboBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) 
         {
             fdo.getInputTypes().set(i, (String) inputTypeComboBox.getSelectedItem());
         }
    });

to

final counter = i;
final JComboBox inputTypeComboBox = new JComboBox(getTypes());
final FunctionDataObject finalFDO = fdo;
inputTypeComboBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) 
         {
             finalFDO.getInputTypes().set(counter, (String) inputTypeComboBox.getSelectedItem());
         }
    });

This link explains why you can only access final variables in inner class

RNJ
  • 15,272
  • 18
  • 86
  • 131
1

You can't access a non final variable in an anonymous class. You could slightly modify your code to work around that restriction (I have made fdo and inputTypeComboBox final and I've also made a final copy of i):

    for (final FunctionDataObject fdo : wdo.getFunctionDataList()) {
        JLabel inputTypesLabel = new JLabel("Input Types: ");
        inputsBox.add(inputTypesLabel);
        for (int i = 0; i < fdo.getNumberOfInputs(); i++) {
            final int final_i = i;
            JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
            final JComboBox inputTypeComboBox = new JComboBox(getTypes());
            inputTypeComboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fdo.getInputTypes().set(final_i, (String) inputTypeComboBox.getSelectedItem());
                }
            });
        }
    }
assylias
  • 321,522
  • 82
  • 660
  • 783
1

This will work:

    for (final FunctionDataObject fdo : wdo.getFunctionDataList()) {
        JLabel inputTypesLabel = new JLabel("Input Types: ");
        inputsBox.add(inputTypesLabel);
        for (int i = 0; i < fdo.getNumberOfInputs(); i++) {
            JLabel inputLabel = new JLabel(fdo.getInputNames().get(i));
            final JComboBox inputTypeComboBox = new JComboBox(getTypes());
            final int index = i;
            inputTypeComboBox.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     fdo.getInputTypes().set(index, (String) inputTypeComboBox.getSelectedItem());
                 }
            });
         }
    }    
Dan D.
  • 32,246
  • 5
  • 63
  • 79