0

This is my code and i would like to know if I can add new buttons to my grid layout every time I click one of the already existing buttons to the instance frame.

public class Board {

        public static void main(String[] args) {
            JButton[] button = new JButton[40];
            int i = 0;
            JFrame frame = new JFrame();
            frame.setLayout(new GridLayout(20, 20, 15, 15));
            while (i < 40) {
                button[i] = new JButton("button" + i);
                button[i].addActionListener(new Action());
                frame.add(button[i]); 
                i++;
            }
            frame.setSize(700, 700);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

        static class Action implements ActionListener{
            @Override
            public void actionPerformed (ActionEvent e){


            }
        }
    }
tmwanik
  • 1,643
  • 14
  • 20
Spyros Chiotakis
  • 239
  • 1
  • 10
  • 2
    Please see this [answer](http://stackoverflow.com/questions/6988317/dynamically-add-components-to-a-jdialog/6988341#6988341). – mre Mar 20 '13 at 23:53

1 Answers1

3

The solution is rather simple.

What you need to do is think about the problem. Firstly, you have a bunch of static references which really aren't required and provide you with little benefit.

Now, having said that. You Action needs some way to know where to add the button. In order to do this, the Action needs a reference to the container you want to add the buttons to...

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Board {

    public static void main(String[] args) {
        new Board();
    }

    public Board() {
        JButton[] button = new JButton[40];
        int i = 0;
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(20, 20, 15, 15));
        Action action = new Action(frame);
        while (i < 40) {
            button[i] = createButton(i);
            button[i].addActionListener(action);
            frame.add(button[i]);
            i++;
        }
        action.setCount(i);
        frame.setSize(700, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public JButton createButton(int index) {

        return new JButton("button" + index);

    }

    public class Action implements ActionListener {

        private JFrame frame;
        private int count;

        public Action(JFrame frame) {
            this.frame = frame;
        }

        public void setCount(int count) {
            this.count = count;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = createButton(count);
            btn.addActionListener(this);
            frame.add(btn);
            frame.revalidate();
            count++;
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366