0

How do i import an int value into an ActionListener and still being able to change it? And not using final since it need to edited.

int colorSnake
public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setTitle("Wormy boi");
        mainFrame.setResizable(true);
        mainFrame.add(new Game(), BorderLayout.CENTER);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
        JMenu menu, options;  
        JMenuItem i1, i2, i3, i4, i5, i6;  
        JMenuBar mb=new JMenuBar();  
        options=new JMenu("Menu");
        menu=new JMenu("Shop");  
        //submenu=new JMenu("Sub Menu"); 
        i1=new JMenuItem("Red");  
        i1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int colorSnake =+ 1;
           }
        });
    });
}
Jakob F
  • 1,046
  • 10
  • 23
  • 2
    Maybe this post could help you: https://stackoverflow.com/questions/11037622/pass-variables-to-actionlistener-in-java – Tempia Andrea Feb 10 '20 at 09:41
  • 5
    Does this answer your question? [Pass variables to ActionListener in Java](https://stackoverflow.com/questions/11037622/pass-variables-to-actionlistener-in-java) – Jakob F Feb 10 '20 at 09:42
  • The same way as for any other function: via function-params (does not work because a listener has fixed args), or via an instance-field. – MakePeaceGreatAgain Feb 10 '20 at 09:55
  • 1
    Well, first of all that code's structure indicates you're quite new to programming so it might be a little harder to get right. Basically you don't _import_ a variable inside that ActionListener but in your case you'd be just using it. However, since your ActionListener doesn't have any reference to an instance of the class containing the main method `colorSnake` would have to be static to be visible (thus the body of `actionPerformed()` might just be `colorSnake++`) or be otherwise accessible (requiring a better design but that's probably out of scope for now). – Thomas Feb 10 '20 at 09:56
  • `int colorSnake =+ 1;` you're creating a local version. Just remove the `int` and it will reference the field of the class. Also, you'll have to make `colorSnake` static, or create an instance of your class. – matt Feb 10 '20 at 11:02
  • Thx for all the help now i get it! – Johannes Löfgren Feb 10 '20 at 12:29

1 Answers1

0

Captured variable in lambda and anonymous classes must be effectively final See : https://stackoverflow.com/a/38471540/7759056

To do what you are aiming for you can create your own ActionListener.

class ColorSnakeActionListener extends ActionListener {
    private int colorSnake;

    @Override
    public void actionPerformed(ActionEvent e) {
        this.colorSnake += 1;
    }

    int getColorSnake(){
        return colorSnake;
    }
}

Keep track on a reference of this ActionListener where you need it.

public class Foo {
    private ColorSnakeActionListener actionListener = new ColorSnakeActionListener();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
             JFrame mainFrame = new JFrame();
             mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             mainFrame.setTitle("Wormy boi");
             mainFrame.setResizable(true);
             mainFrame.add(new Game(), BorderLayout.CENTER);
             mainFrame.pack();
             mainFrame.setLocationRelativeTo(null);
             mainFrame.setVisible(true);
             JMenu menu, options;  
             JMenuItem i1, i2, i3, i4, i5, i6;  
             JMenuBar mb=new JMenuBar();  
             options=new JMenu("Menu");
             menu=new JMenu("Shop");  
             //submenu=new JMenu("Sub Menu"); 
             i1=new JMenuItem("Red");  
             i1.addActionListener(actionListener);
         });
    }

}

This class can also be a private inner class if you want to keep your implementation free.

Axiome
  • 695
  • 5
  • 18
  • You're making the same error. – matt Feb 10 '20 at 11:02
  • I apologize on my over interpretation, I am certainly the one being rude here. I just wanted to point out the fact that your first comment was not helping me to find out the mistake I made. All is fine now, let's move on. Again i'm sorry for this misunderstanding. Have a nice day! – Axiome Feb 10 '20 at 11:52