The_Cleric_
The_Cleric_

Reputation: 21

Recording which array is chosen in an array of JButtons

My trouble is coming from trying to record which array is being chosen. As in, if I have an array of 5 elements, recording that the 3rd element is being chosen. To put into context, I have 2 different arrays each with 10 elements; one array is made up of integers (called money), and the other is an array of JButtons.

public class Game extends JFrame {

JPanel cases = new JPanel(new GridLayout(2, 5)); //Section containing game cases
JButton[] caseButton = new JButton[10];                                   // Declaration of the
String gameCase[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};  //    case buttons
int[] money = {1, 2, 5, 10, 100, 1000, 5000, 10000, 20000, 30000}; //money values
}

public void StartGame() {
    Shuffle(money); //Shuffle the money
    //Initialising case buttons
    for (int i = 0; i < caseButton.length; i++) {
        caseButton[i] = new JButton(gameCase[i]);
        cases.add(caseButton[i]);
        caseButton[i].setPreferredSize(new Dimension(100, 100));
        caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));
        caseButton[i].setForeground(new Color(255, 215, 0));
        caseButton[i].setActionCommand(gameCase[i]);
    }

The Money array is being shuffled at the start of the program to allow for a different order on each run. What I want to know is when one of these JButtons is selected, how can I record which Array element was selected? So I can then do stuff with the corresponding money array. (Eg, the 7th JButton is chosen, I want to then change the text of the JButton to the number held in the 7th money array.) Help would be greatly appreciated. This is due next week and there's still so much more that needs to be done.

Upvotes: 2

Views: 85

Answers (2)

khachik
khachik

Reputation: 28703

Alternative to the custom button class solution, you can keep the buttons in a map, with their indexes and on the action handler, get the index based on the source:

Map<JButton, Integer> caseButtons = new HashMap<>(10);
for(int i=0; i<10; i++) {
    JButton button = ...
    // all the other setup goes here
    caseButtons.put(button, i);
}
...

public void actionPerformed(ActionEvent e){
    // Get the source of the click as a MyButton
    JButton source = (JButton) e.getSource();
    int index = caseButtons.get(source);
    ...
}

Upvotes: 0

acn3
acn3

Reputation: 358

I would recommend creating a custom button class that holds the index of the corresponding value in the money array. For example:

public class MyButton extends JButton {
    private int moneyIndex;

    public MyButton(String text, int moneyIndex){
        super(text);
        this.moneyIndex = monexIndex;
    }

    public int getMoneyIndex(){
        return moneyIndex;
    }
}

Then, you can create a button in the same way you did before, but pass it the money index:

for (int i = 0; i < caseButton.length; i++) {
    // I suspect you want the moneyIndex to match the index of the button
    caseButton[i] = new MyButton("?", i);

    cases.add(caseButton[i]);

    // These can be moved to the custom button class if all MyButtons have these customizations
    caseButton[i].setPreferredSize(new Dimension(100, 100));
    caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));
    caseButton[i].setForeground(new Color(255, 215, 0));

    // Set this class as the action listener
    caseButton[i].setActionListener(this);
}

Then, in your action listener (I assume your main class already extends ActionListener), you can access the moneyIndex variable:

public void actionPerformed(ActionEvent e){
    // Get the source of the click as a MyButton
    MyButton source = (MyButton) e.getSource();

    // Get the moneyIndex of the source button
    int moneyIndex = source.getMoneyIndex();

    // Update the button's text according to the moneyIndex
    source.setText(Integer.toString(money[moneyIndex]));
}

This approach has the advantage that the index is stored by the button, so you don't need to search through all the buttons to check which has been pressed. This is more important as the number of buttons you have increases, but it's something to think about at any size.

Additionally, this method will make your life easier when this project gets more complicated, as each button can store information that is specific to it without a need for a bunch of arrays or action commands.

Upvotes: 1

Related Questions