简体   繁体   English

jcheckbox上的actionListener

[英]actionListener on jcheckbox

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. 我正在尝试向JCheckBox添加匿名actionListener,但是在访问要更新其值的对象时遇到一些困难。 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): 我想做的是下面的(我删除了一些gui代码以使其更易于阅读):

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());
             }
        });
     }
}    

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 链接说明了为什么只能访问内部类中的最终变量

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 ): 您可以稍微修改代码以解决该限制(我将fdoinputTypeComboBox final,并且还制作了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());
                }
            });
        }
    }

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());
                 }
            });
         }
    }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM