spellz
spellz

Reputation: 5

Get buttons already added in a JFrame

I'm developing a simple 15-puzzle game. I added 16 generated numbered buttons to a JFrame and that was the exercise (University). I'm now trying to go further and make interactions so I need to get all buttons and put them into a 2D vector in order to calculate where user clicks and if and where the cell could "slide", but I don't know how to get them from the Frame.

Here is the generator code:

public void generation(){
    int num;
    Random rand = new Random();
    ArrayList<String> list = new ArrayList<String>();

    for(int i = 0; i < this.getTot(); i++)
        list.add(""+ i);

    while(!list.isEmpty()){
        do{                     
            num = rand.nextInt(this.getTot());
        } while (!list.contains("" + num));

        list.remove("" + num);

        if(num == 0){
            this.add(new Button(" ");
        }
        else{
            this.add(new Button("" + num);                  
        }
    }
}

And here is the constructor:

public Base15(int x, int y){
    this.setSize(WIDTH, HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setLayout(new GridLayout(x, y));

    this.x = x;
    this.y = y;

    this.generation();
    this.cells = new Button[x][y];  
}

Thank you.

UPDATE: Followed ufis suggestions and had the 2d array done!

for (int row = 0; row < x; row++){
        for (int col = 0; col < y; col++){
            do{                     
                num = rand.nextInt(this.getTot());
            } while (!list.contains("" + num));

            list.remove("" + num);

            if(num == 0){
                cells[row][col] = new Button(" ", sw, label);
                this.add(cells[row][col]);

            }else{
                cells[row][col] = new Button("" + num, sw, label);
                this.add(cells[row][col]);

            }
        }

Thank you all!

Upvotes: 0

Views: 2751

Answers (2)

Juan_H
Juan_H

Reputation: 387

public void resetPanel(JFrame form)
{
    Component[] components = form.getContentPane().getComponents();  
    for(Component component : components)
    {
        if(component instanceof JButton){

            JButton button = (JButton) component;
            button.setText("?");


        }
    }
}

Upvotes: 1

ufis
ufis

Reputation: 176

Unless I completely misunderstand your question you can do

JFrame theFrame = new JFrame();
// lots of code here to add buttons
Component[] components = theFrame.getComponents();
for (Component component : components) {
    if (component instanceof Button) {
        // do something
    }
}

But it would be better if you store some reference to all your Buttons as you create / add them to the Frame.


EDIT: I think you should check the way you create your Buttons when you add them to the frame.

When you do

JFrame theFrame = new JFrame();

for (int i = 0; i < 15; i++) {
    theFrame.add(new Button());
}

You have no reference to your Buttons. This is why you need to "get the Buttons from the Frame.

If you do something like

JFrame theFrame = new JFrame();
Button[] buttons = new Button[15];

for (int i = 0; i < 15; i++) {
    buttons[i] = new Button();
    theFrame.add(buttons[i]);
}

You will not have to loop through all the components at a later stage, because you have reference to the buttons in your buttons array. You can of course make that a Button[][] too. But the win here is that you have the reference to the list of buttons at creation time.

Upvotes: 2

Related Questions