How can I give a variable to an action listener?
        Posted  
        
            by Roman
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Roman
        
        
        
        Published on 2010-03-21T17:37:58Z
        Indexed on 
            2010/03/21
            17:41 UTC
        
        
        Read the original article
        Hit count: 372
        
I have a static variable partner in the class. And I want to set a value of these variable whenever a radio button is pressed. This is the code I tried to use:
for (String playerName: players) {
    option = new JRadioButton(playerName, false);
    option.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent evt) {
            partner = playerName;
        }
    });
    partnerSelectionPanel.add(option);
    group.add(option);
}
The problem here is that the actionPerformed does not see the variable playerName created in the loop. How can I pass this variable to the actionListener?
© Stack Overflow or respective owner