Adding action to a JButton when using a for loop

arraylist, java, jbutton, jpanel, swing

Solution

There is nothing listening for when the buttons are pressed. When you create each button, you must give them action listeners.

Also, you should be adding the same button that you create.

buttons[i] = new JButton("" + a_btnNames[i]);
//buttons[i] should now be added to the panel

New for loop should be...

for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton("" + a_btnNames[i]); //create button & add to array
    buttons[i].addActionListener(this); //add an action listener to the current button
    panel.add(buttons[i]); //add that same button to the panel

}

Problem

I'm trying to dynamically add buttons (JButtons), which changes names everytime. I doing it with a for loop and is not really problematic. but when adding an action listener or identifying which button got pressed, that is when things don't work so good. MyFrame.java ``` import javax.swing.*; import java.awt.event.*; import java.awt.GridLayout; public class MyFrame extends JFrame implements ActionListener { private JPanel panel; private static JButton[] buttons = new JButton[18]; // set all static calculate JButtons private static JButton equalsBtn, addBtn, subBtn, multiBtn, divBtn, clearBtn, plusMinusBtn, decimalBtn; // set all static number JBtuttons private static JButton zeroBtn, oneBtn, twoBtn, threeBtn, fourBtn, fiveBtn, sixBtn, sevenBtn, eightBtn, nineBtn; private static JTextField resultField; // numOne is the first row of figures en the second numSecond is the second row private static double numOne, numSecond, result; private static double plusMinus; private static int addClick = 0, subClick = 0, multiClick = 0, divClick = 0; private static int clearField; public MyFrame() { // configure the JFrame super("Rekennen maar!"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setSize(230, 370); setLocationRelativeTo(null); // confugure the JPanel panel = new JPanel(); panel.setSize(230, 370); panel.setLayout(new GridLayout(5, 0)); // array string whit all the button names String a_btnNames[] = {"clearBtn", "plusMinusBtn", "divBtn", "multiBtn", "sevenBtn", "eightBtn", "nineBtn", "subBtn", "fourBtn", "fiveBtn", "sixBtn", "addBtn", "oneBtn", "twoBtn", "threeBtn", "equalsBtn", "zeroBtn", "decimalBtn"}; // array String whit all button characters String a_btnCharts[] = {"C", "+/-", "/", "*", "7", "8", "9", "-", "4", "5", "6", "+", "1", "2", "3", "=", "0", "."}; for(int i = 0; i < buttons.length; i++) { // make new button name buttons[i] = new JButton(a_btnNames[i]); // add button to panel panel.add(new JButton(a_btnCharts[i])); //System.out.println(buttons[i]); } // add panel when he's filled add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { // press the button if ( e.getSource() == ...... ) { System.out.println("123"); } } } ``` Main.java ``` public class Main { public static void main(String[] arg) { MyFrame mf = new MyFrame(); } } ```

Original source