'Using getActionCommand String to substring into a row and column

I am working on a task where I create a GUI with Java. Currently, I am making a layout for Minesweeper - but I'm a bit puzzled with this method's instructions.

The instructions go as follows:

mousePressed method

  • getActionCommand to get the String
  • use getComponent and cast it to a button
  • break the actionCommand String into a row and column
  • set the button text of the row and column to "!"

This is what I have for mousePressed()

    public void mousePressed(MouseEvent b)
    {
        
        Component a = b.getComponent(); 
        JButton x = (JButton)a;
        System.out.println(x.getActionCommand());
        String s = x.getActionCommand();
        // substring for row and col
        // set text
    }

How would I do the last two? Especially with

break the actionCommand String into a row and column

set the button text of the row and column to "!"

With substring, how would I go and "break" the actionCommand String into a row and column? (Break is not meant to be taken literally). Substring returns part of a string with a start index and end index, but what string from actionCommand would I use for the substring? And how would I set the row and column to show text as "!"? Would I need to use substring for this as well?

Here is my entire code for the layout so far.

import java.awt.GridLayout;
import java.awt.Component;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GridOfButtons extends JFrame implements MouseListener
{
    private JButton [][] grid;

    public GridOfButtons()
    {
        grid = new JButton[10][10];
        BuildGUI();
    }
    
    public void BuildGUI()
    {
        setSize(1200, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(10, 10));
        setVisible(true);

        for(int r = 0; r < grid.length; r++)
        {
            for(int c = 0; c < grid[r].length; c++)
            {
                grid[r][c] = new JButton("*");
                grid[r][c].setActionCommand(r + ":" + c);
                addMouseListener(this);
                getContentPane().add(grid[r][c]);
            }
        }
    }

    public void mouseClicked(MouseEvent b)
    {
    }

    public void mouseEntered(MouseEvent b)
    {
    }

    public void mouseExited(MouseEvent b)
    {
    }

    public void mouseReleased(MouseEvent b)
    {
    }   

    public void mousePressed(MouseEvent b)
    {
        
        Component a = b.getComponent(); 
        JButton x = (JButton)a;
        System.out.println(x.getActionCommand());
        String s = x.getActionCommand();
        // substring for row and col
        // set text
    }

    public static void main(String[] args)
    {
        new GridOfButtons();
    }
}

These are also the instructions I followed:

Create a class that is a subclass of JFrame that uses interface MouseListener with:

Instance variables:

  • a 2d array of JButtons

Default constructor:

  • sets the size of the array to 10 by 10
  • calls a method to build the GUI

GUI builder method:

  • set default close
  • sets the layout to GridLayout
  • use nested for loops to create the button with initial text "*"
  • use setActionCommand to add the row + ":" + column
  • add the Mouse listener
  • add the button to the JFrame

mousePressed method:

  • getActionCommand to get the String
  • use getComponent and cast it to a button
  • break the actionCommand String into a row and column
  • set the button text of the row and column to "!"

I can't strictly adhere to the text (I'm adding other lines of code that aren't clarified in the instructions), but I can't do anything too advanced either since I'm working under constraints.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source