JRadioButton and action on them

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.


This class I thought to handle all button events.

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(new View(fullRandom), BorderLayout.CENTER);
            Minesweeper.game.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));
        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?

1 answer

  • answered 2017-06-17 18:26 Steamwork

    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:


    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:


    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 :)