2

I wrote this code to make the choices a user has to make to choose the game modes:

JButton btnNewButton = new JButton("Start Game");

JRadioButton beginner = new JRadioButton("Beginner");
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
JRadioButton mineFullRandom = new JRadioButton("Mine full random");
JRadioButton minePartialRandom = new JRadioButton("Mine partial random");

The first three are used to choose the difficulty of the game while the last two to choose the mode. I set just selected the beginner difficulty and the mine full random modality.The Start Game button, as you can understand from the name it is necessary to start the game

Once I have created the JRadioButton and the JButton I am going to add them on a GroupLayout.

Controller.java

This class I thought to handle all button events.

@Override
public void actionPerformed(ActionEvent e) {

    JButton source = (JButton)e.getSource();
    JRadioButton difficulty = (JRadioButton)e.getSource();
    JRadioButton choice = (JRadioButton)e.getSource();

    if(source.getText().equals("Start Game")){
        if(difficulty.getText().equals("Beginner") /*&& choice.getText().equals("Mine full random")*/){
            fullRandom = new FullRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
            View view = new View(fullRandom);
            //Minesweeper.game.container.add(view);
            /*Minesweeper.game.container.add(new View(fullRandom), BorderLayout.CENTER);
            Minesweeper.game.container.remove(Minesweeper.menu);
            Minesweeper.game.setVisible(true); */
            view.setVisible(true);
        }
        else if(difficulty.getText().equals("Beginner") && choice.getText().equals("Mine partial random")){
            partialRandom = new PartialRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
            Minesweeper.game.container.add(new View(partialRandom));
            Minesweeper.game.container.remove(Minesweeper.menu);
        }
        else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine full random")){
            fullRandom = new FullRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);

        }
        else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine partial random")){
            partialRandom = new PartialRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);

        }
    }
}

How do I do when you select a specific JRadioButton, do you deselect the one previously selected? How do I get the game started and close the menu when the 'Start Game` button is pressed?

  • The first three lines of your listener do not make sense. The value of the variables 'source", "difficulty", and "choice" are all going to be the same thing - the button that was pressed. – FredK Jun 17 '17 at 18:38
  • 3
    As to deselecting previous buttons,you don't. Look up the documentation for [ButtonGroup](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html). – FredK Jun 17 '17 at 18:42

1 Answers1

2

The issue you could be facing is that the source you obtain through the ActionListener event will be the same throughout for source, difficulty and choice. Firstly, you should have a group to store the radio buttons:

ButtonGroup difficultyGroup = new ButtonGroup();

When you add the JRadioButtons to this group, selecting one option will automatically deselect all others:

difficultyGroup.add(beginner);
difficultyGroup.add(intermediate);
difficultyGroup.add(expert);
...

This ensures only one button is selected at a time. Moreover, addressing the issue revolving around the event received -

Adding an ActionCommand to each radio button will allow you to perform operations with the buttons through a string you assign to each of them. For example, before you add the buttons to the group, assign the following values:

beginner.addActionCommand("Beginner");
intermediate.addActionCommand("Intermediate");

Keep in mind that the following strings are for internal purposes only. This implies that you can identify the buttons through their assigned String values

public void ActionPerfomed(ActionEvent e) {
    // To get the source (for the game start button)
    JButton source = (Jbutton) e.getSource();

    // Get the radio button source which was selected
    // Resultantly obtain the String which it represents
    String difficulty = difficultyGroup.getSelection().getActionCommand();
}

Now you can use the difficulty String variable in your conditional if statements to perform those operations safely.

Hope this helped :)