Home > Mobile >  Using getActionCommand String to substring into a row and column
Using getActionCommand String to substring into a row and column

Time:03-08

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.

CodePudding user response:

I am aware that with assignments you are often limited to use only the things that you have learned so far. Hence I understand that you can only use method substring (of class java.lang.String) to manipulate the text of each JButton. (I hope you can also use method indexOf because I use it in the below code.)

Apart from the [missing] code to change the JButton text, your code has two problems.

  1. Calling method setVisible should be the last line of your BuildGUI method.
  2. You are adding a MouseListener to the JFrame, i.e. to class GridOfButtons. You need to add a MouseListener to each JButton.

Here is your corrected code including the code to change the JButton text. As stated above, I moved the line setVisible(true);. I also added a MouseListener to each JButton. (See the comment CHANGE HERE in the below code.) And I added code to change the JButton text in method mousePressed.

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;

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

        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);
                grid[r][c].addMouseListener(this); // CHANGE HERE
                getContentPane().add(grid[r][c]);
            }
        }
        setVisible(true);
    }

    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
        int ndx = s.indexOf(':');
        if (ndx >= 0) {
            String row = s.substring(0, ndx);
            String col = s.substring(ndx   1);
            String text = row   '!'   col;
            x.setText(text);
        }
    }

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

Some notes regarding the above code.

  1. It is recommended to use screen capture of running app

  • Related