Reputation: 1261
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());
}
});
}
}
Upvotes: 0
Views: 556
Reputation: 32391
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());
}
});
}
}
Upvotes: 1
Reputation: 328598
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());
}
});
}
}
Upvotes: 1
Reputation: 15552
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
Upvotes: 1