I am using a for loop to populate an 8x8 grid with two different buttons randomly. I have an array of instances of the buttons and for some reason, it is only giving me one of each button and not populating the rest of the grid with anything. Any advice? I included only the buildPanel block of the code but I do have separate classes for the buttons and the Game class. I also have the rest of the code somewhat done. It doesn't fully do what I need yet and needs to be cleaned up but if it might be helpful in solving this issue, I can post it. I'm unable to post more code than this in the question without writing more details but it's pretty straightforward of a question I think.
private void buildPanel()
{
// Create labels to display the
treasuresLeftLabel = new JLabel("Treasures left: ");
treasuresFoundLabel = new JLabel("Treasures found: ");
triesLeftLabel = new JLabel("Tries left: ");
// Create text fields for each label
treasuresLeftTextField = new JTextField(2);
treasuresLeftTextField.setEditable(false);
treasuresLeftTextField.setText(String.valueOf(20-game.getTreasuresFound()));
treasuresFoundTextField = new JTextField(2);
treasuresFoundTextField.setEditable(false);
treasuresFoundTextField.setText(String.valueOf(game.getTreasuresFound()));
triesLeftTextField = new JTextField(2);
triesLeftTextField.setEditable(false);
triesLeftTextField.setText(String.valueOf(game.getTriesLeft()));
emptyButton = new EmptyButton();
emptyButton.addActionListener(new emptyButtonListener());
treasureButton = new TreasureButton();
treasureButton.addActionListener(new treasureButtonListener());
// new JPanel object referenced by panel
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("Treasure Hunt", JLabel.CENTER), BorderLayout.NORTH);
statsPanel = new JPanel();
statsPanel.setLayout(new GridLayout(3, 2));
statsPanel.add(treasuresLeftLabel);
statsPanel.add(treasuresLeftTextField);
statsPanel.add(treasuresFoundLabel);
statsPanel.add(treasuresFoundTextField);
statsPanel.add(triesLeftLabel);
statsPanel.add(triesLeftTextField);
panel.add(statsPanel, BorderLayout.WEST);
JButton[] buttons = new JButton[62];
for (int index = 0; index < 62; index )
{
if (index < 20)
{
buttons[index] = treasureButton;
}
else
{
buttons[index] = emptyButton;
}
}
gameGridPanel = new JPanel();
gameGridPanel.setLayout(new GridLayout(8, 8));
// Generate a random number between 0 and 61
int randomNumber = new Random().nextInt(62);
for (int count = 0; count < 62; count )
{
if (buttons[randomNumber] != null)
{
gameGridPanel.add(buttons[randomNumber]);
randomNumber = new Random().nextInt(62);
}
else
{
randomNumber = new Random().nextInt(62);
}
}
panel.add(gameGridPanel, BorderLayout.CENTER);
}
CodePudding user response:
it is only giving me one of each button
Because you only have a single instance of each button:
for (int index = 0; index < 62; index )
{
if (index < 20)
{
buttons[index] = treasureButton; // single instance
}
else
{
buttons[index] = emptyButton; // single instance
}
}
You need to create a new instance when you add it to the array.
for (int index = 0; index < 62; index )
{
if (index < 20)
{
//buttons[index] = treasureButton;
buttons[index] = new TreasureButton();
}
else
{
//buttons[index] = emptyButton;
buttons[index] = new EmptyButton();
}
}
Also an easier way to randomize the buttons is to:
- Add the button to an
ArrayList
- Use the
Collections.shuffle(...)
method to randomize the order of the buttons - Iterate through the
ArrayList
and add each button to the grid