Skip to content
Advertisement

How would I use the radio button in jswing to make a listener capable of placing an S or O in the given cell depending on what is selected?

package SOS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class BoardPanel {
    private JFrame frame;

    private void createAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void createBoard(ActionEvent event) {
        Object source = event.getSource();
        if (source instanceof JTextField) {
            JTextField textField = (JTextField) source;
            String text = textField.getText();
            int dimension = Integer.parseInt(text);
            JPanel board = new JPanel(new GridLayout(dimension, dimension));
            for (int row = 0; row < dimension; row++) {
                for (int col = 0; col < dimension; col++) {
                    JLabel square = new JLabel("   ");
                    square.setBackground(Color.white);
                    square.setOpaque(true);
                    square.setBorder(BorderFactory.createLineBorder(Color.black));
                    board.add(square);
                }
            }
            frame.add(board, BorderLayout.CENTER);
            frame.pack();
        }
    }

    private JPanel createTopPanel() {
        
         JRadioButton optionS = new JRadioButton("S");
         JRadioButton optionO = new JRadioButton("O");
         
         ButtonGroup group = new ButtonGroup();
         group.add(optionS);
         group.add(optionO);
        
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Board size:");
        topPanel.add(label);
        JTextField boardSize = new JTextField(6);
        boardSize.addActionListener(this::createBoard);
        topPanel.add(boardSize);
        topPanel.add(optionS, BorderLayout.NORTH);
        topPanel.add(optionO, BorderLayout.CENTER);
        return topPanel;
        
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
    }
}

I know I am supposed to make an action listener for each radio button and that seems pretty straightforward, but I am confused on how I would do that and then translate that to placing an S or O on the game boards given cell depending on what is selected. I think the more confusing part is being able to draw in the given cell either the S or the O depending on what is selected. This is for an SOS game, sort of like tic tac toe. I tried following a simple tic tac toe example but got lost as there is no radio buttons like this and I am using a different createboard method.

Advertisement

Answer

You can declare your radioButton globally so you can check the selected status. Then in a listener in your cells check that and place the appropriate letter:

package SOS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class BoardPanel {

    private JFrame frame;
    private JRadioButton optionS;
    private JRadioButton optionO;

    private void createAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(createTopPanel(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void createBoard(ActionEvent event) {
        Object source = event.getSource();
        if (source instanceof JTextField) {
            JTextField textField = (JTextField) source;
            String text = textField.getText();
            int dimension = Integer.parseInt(text);
            JPanel board = new JPanel(new GridLayout(dimension, dimension));
            for (int row = 0; row < dimension; row++) {
                for (int col = 0; col < dimension; col++) {
                    JLabel square = new JLabel("   ");
                    square.setBackground(Color.white);
                    square.setOpaque(true);
                    square.setBorder(BorderFactory.createLineBorder(Color.black));
                    board.add(square);
                    square.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            square.setText(optionS.isSelected() ? " S " : " O ");
                        }
                    });
                }
            }
            frame.getContentPane().add(board, BorderLayout.CENTER);
            frame.pack();
        }
    }

    private JPanel createTopPanel() {

        optionS = new JRadioButton("S");
        optionO = new JRadioButton("O");

        ButtonGroup group = new ButtonGroup();
        group.add(optionS);
        group.add(optionO);

        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Board size:");
        topPanel.add(label);
        JTextField boardSize = new JTextField(6);
        boardSize.addActionListener(this::createBoard);
        topPanel.add(boardSize);
        topPanel.add(optionS, BorderLayout.NORTH);
        topPanel.add(optionO, BorderLayout.CENTER);
        return topPanel;

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
    }
}
Advertisement