Search code examples
javaswingloopsjbuttonactionlistener

How to create N ActionListener with for loop?


I have been created N(unknown SIZE) Jbuttons with for loop and placed them in the frame. I am trying to display the message "Button number x" by clicking on button number x. On the second for loop, when I trying to create N ActionListener I have some error.

Error

Local variable I defined in an enclosing scope must be final or effectively final

Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class program extends JFrame{
    final int N = 10;
    final int JUMPS = 210;
    private JButton[] buttons = new JButton[N];

    public program(){
        super("test");
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < N; i++) {
            buttons[i] = new JButton("" + i);
            buttons[i].setBounds((i % 4) * JUMPS, (i / 4) *JUMPS , 200, 200);
            add(buttons[i]);
        }

        for (int i = 0; i < N; i++) {
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Button number " + i);
                }
            });
        } 

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        program mainGUI = new program();
    }
}

Did you have any solution for the error?


Solution

  • You could do

    JOptionPane.showMessageDialog(null, "Button number " + e.getActionCommand());