(Java GUI)Changing the Text of a JButton once its pressed.

  • Context: Comp Sci 
  • Thread starter Thread starter S.Parker
  • Start date Start date
  • Tags Tags
    Text
Click For Summary

Discussion Overview

The discussion revolves around a Java GUI programming issue related to changing the text of a JButton in a Minesweeper game when the button is pressed. The focus is on the implementation details of using ActionListener within a 2D array of buttons.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their struggle with displaying a "0" on a JButton after it is pressed, indicating a lack of experience with Java GUI programming.
  • Another participant suggests that the issue arises because the loop variables i and j exceed the array bounds when the actionPerformed method is called, potentially leading to an index out of range exception.
  • This same participant recommends using the ActionEvent parameter to correctly identify which button was pressed instead of relying on the loop variables.
  • A later reply indicates that the original poster resolved their issue independently, expressing gratitude for the assistance.

Areas of Agreement / Disagreement

There is a general agreement on the nature of the problem related to variable scope and button identification, but the discussion does not explore multiple competing views or unresolved issues, as the original poster successfully found a solution.

Contextual Notes

The discussion does not delve into specific limitations or assumptions beyond the immediate problem of button text updating.

S.Parker
Messages
4
Reaction score
0

Homework Statement


Im a big rookie in Java ( and pretty much every language). I am trying to make a Minesweeper game for an assignment and I am still in the beta stages. I am stuck however on how to display the mines as a "0" once the button is pressed. I created the Jbuttons using a 2d array, but I can't seem to put the "0" on the specific button I want.

Here is my code ( please excuse the ugliness/ probable inefficiency) I really am clueless when it comes to GUI's.


Homework Equations


Code:
import java.awt.*;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Grid extends JPanel {
    // Initializes rows,columns and mines
    int rows = 10;
    int cols = 10;
    int i, j = 0;
    int mines = 10;
    boolean[][] setmine = new boolean[rows][cols];
    boolean[][] clickable = new boolean[rows][cols];
    private JToggleButton squares[][], squares2[][];

    // Contructor for creating a grid(with default size 400x400
    public Grid() {
        this.setSize(600, 600);
        this.setLayout(new GridLayout(rows, cols));
        squares = new JToggleButton[rows][cols];
        buildButtons();
    }

    private void buildButtons() {
        // loops are used for creating the "buttons" on the grid.
        int MinesNeeded = 10;
        // builds buttons
        // ----------------------------------------
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                squares[i][j] = new JToggleButton();
                // squares[i][j].setEnabled(false);
                squares[i][j].setSize(600, 600);

                // --------------------------------------------------
                // This part randomises the mines
                // -----------------------------------------------------
                while (MinesNeeded > 0) {

                    int x = (int) Math.floor(Math.random() * rows);
                    int y = (int) Math.floor(Math.random() * cols);
                    if (!setmine[x][y]) {
                        setmine[x][y] = true;

                        MinesNeeded--;
                    }
                }
                // ----------------------------------------------------------------------------

                this.add(squares[i][j]);

                if (setmine[i][j] == true) {

                    squares[i][j].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            // this is the problem
                            squares[i][j].setText("0");
                        }
                    });
                }
            }
        }
    }

    public static void main(String[] args) {
        Grid g = new Grid();
        JFrame frame = new JFrame("Minesweeper");

        frame.add(g);
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The Attempt at a Solution

 
Physics news on Phys.org
I think the problem is that when actionPerformed is called, you have already completely gone through your loops, and so i=j=10, and of course, squares[10][10] will throw an index out of range exception. Instead of using squares[j] in that method, try utilizing the method parameter ae in order to set the text on the correct button.
 
gabbagabbahey said:
I think the problem is that when actionPerformed is called, you have already completely gone through your loops, and so i=j=10, and of course, squares[10][10] will throw an index out of range exception. Instead of using squares[j] in that method, try utilizing the method parameter ae in order to set the text on the correct button.


Thank you for the swift reply. I am almost 100% sure that is the problem. Could you guide me on how to use the method parameter to set the text? (I don't understand what the method parameter does in Action Listener ( I used ActionListener for the first time yersterday))
 
Nevermind, I got it to work. Thanks a lot
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K