0

익명의 actionListener를 JCheckBox에 추가하려고하지만 값을 업데이트하려는 객체에 액세스하는 데 어려움이 있습니다. 나는 비 최종적인 것에 관해서 오류를 계속하고, 내가 최종적으로 그것을 바꿀 때 다른 것들에 대해 불평한다.
은 무엇 메신저 같습니다하려고 노력 (내가 쉽게 읽을 수 있도록 GUI를 코드의 일부를 제거했습니다) :actionListener on jcheckbox

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

나는 당신이 약간 (내가 fdoinputTypeComboBox 마지막을 만들었습니다와 나는 또한 i의 최종 사본을했습니다) 그 제한을 해결하기 위해 코드를 수정할 수 있습니다 이 문제는 제시되지 않은 코드에 숨겨져 있다고 생각합니다. [SSCCE] (http://sscce.org/)에 더 빨리 도움을주기 위해 익명의 최종 지표를 설정하는 방법이 아니라 코드 개념에 실수가있을 수 있습니다. 청취자 – mKorbel

답변

1

익명 클래스의 최종 변수가 아닌 변수는 액세스 할 수 없습니다.

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

대단히 감사합니다. 대단히 감사합니다! – user1584120

1

업데이트

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

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

에서 코드

This 링크는 내부 클래스의 최종 변수에만 액세스 할 수있는 이유를 설명합니다.

+0

'i'도 최종해야합니다. – assylias

+0

감사합니다 @assylias 지금 업데이트했습니다. – RNJ

1

이 작동합니다 :

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