Okay, so I think it will be easier to show you then explain. I am making Minesweeper, I have 100 toggle buttons, I have a randomizer to place 10 bombs.

ArrayList<Integer> bomb = new ArrayList<Integer>();
        Random rand = new Random(100);
        for (int i = 0; i < 10; i++) {
        bomb.add(rand.nextInt());
        (jToggleButton+bomb.get(i)).setText("B");

That doesn't work above, but do you see what I am asking? Can I somehow concatenate jToggleButton + (the random number chosen) to make like JToggleButton12 or 23 or 76, ect..


Thanks!
-Austin

Recommended Answers

All 8 Replies

Okay, so I think it will be easier to show you then explain. I am making Minesweeper, I have 100 toggle buttons, I have a randomizer to place 10 bombs.

ArrayList<Integer> bomb = new ArrayList<Integer>();
        Random rand = new Random(100);
        for (int i = 0; i < 10; i++) {
        bomb.add(rand.nextInt());
        (jToggleButton+bomb.get(i)).setText("B");

That doesn't work above, but do you see what I am asking? Can I somehow concatenate jToggleButton + (the random number chosen) to make like JToggleButton12 or 23 or 76, ect..


Thanks!
-Austin

Why not just have an array of jToggleButtons:

JToggleButton[] buttons = new JToggleButton[5];
...
buttons[i]=new JToggleButton...setText("B");

and iterate through all the toggle buttons setting their text appropriately? instead of trying to create a bunch of them with their own object names? you can get the different buttons by getText() i reckon

But I would still have to toggle 10 random buttons to have a bomb on them.

But I would still have to toggle 10 random buttons to have a bomb on them.

see edited post... also then you would do maybe

Random randomGenerator = new Random();
//for statement to iterate 10times
...
int tmp=randomGenerator.nextInt(10);
..//here you would need code to make sure that the number tmp hasnt already been generated
buttons[tmp]=...;

I get what you're saying, I just think I am missing something still...

How am I exactly going to choose that particular button? I need to have the bombs in an array for the engine itself, but I also need to make sure the button themselves are set with a bomb object so that GUI lines up with the user.

Do you follow? or not really?


Like jToggleButton(add random number to this to match the existing variable for this button already).setText();

I get what you're saying, I just think I am missing something still...

How am I exactly going to choose that particular button? I need to have the bombs in an array for the engine itself, but I also need to make sure the button themselves are set with a bomb object so that GUI lines up with the user.

Do you follow? or not really?

use the method setActionCommand to give all your bomb buttons a specific name or if you text 'B' is for all the bombs then you'd just loop through the array of buttons looking for all those that have 'B' or a certain action command by getActionCommand() and then you;ll know which buttons are bombs and which are not. I'd say use the setActionCommand thought then your action listener for the bomb buttons will all be the same.

Can you direct me to example that would relate to my scenario with this setActionCommand? Or is this the same concept of action/listener?


I gotta head off to a 3 hours BIO lab, I shall be back on here after that.
Thanks so far mate!

Can you direct me to example that would relate to my scenario with this setActionCommand? Or is this the same concept of action/listener?


I gotta head off to a 3 hours BIO lab, I shall be back on here after that.
Thanks so far mate!

See here:http://www.java2s.com/Tutorial/Java/0260__Swing-Event/GetandsetActionCommand.htm and here:http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Button.html#getActionCommand()

I need to have the bombs in an array for the engine itself, but I also need to make sure the button themselves are set with a bomb object so that GUI lines up with the user.

That's good - proper separation of GUI and engine/model.
You can use JComponent's putClientProperty/getClientProperty methods to associate any object(s) with your buttons. You can therefore have an array of buttons, each one using its clientProperty to link directly to the correct cell in the engine (no messing about fudging text strings). You can loop through the array of buttons referring to the linked engine cell to set the buttons text/icon or whatever appropriately. You can use the same action listener for all the buttons, and event.getSource().getClientProperty(...) to get the engine cell associated with the clicked button.

With this architecture all the state info and all the game logic is in the engine. The buttons just query the corresponding engine cell to control their appearance and behaviour.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.