Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:08+00:00 2026-06-17T19:53:08+00:00

I am trying to build a minesweeper type game, more specifically the two player

  • 0

I am trying to build a minesweeper type game, more specifically the two player one that used to feature on MSN games. I have a multidimensional array of Tile objects. Each Tile has a state (mine, blank or number of mines adjacent). I have a GUI class that handles all of the front end aspects of the program.

Each Tile extends JButton and implements MouseListener, however when I click on a button it does not fire the MouseClicked method for the corresponding button/tile.

Code is as follows :

public class Tile extends JButton implements MouseListener {

private int type;

public Tile(int type, int xCoord, int yCoord) {
    this.type = type;
    this.xCoord = xCoord;
    this.yCoord = yCoord;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println("Clicked");
}

}

And the GUI class :

public class GUI extends JPanel {

JFrame frame = new JFrame("Mines");
private GameBoard board;
private int width, height;

public GUI(GameBoard board, int width, int height) {
    this.board = board;
    this.width = width;
    this.height = height;
    this.setLayout(new GridLayout(board.getBoard().length, board.getBoard()[0].length));
    onCreate();
}

private void onCreate() {
    for (int i = 0; i < board.getBoard().length; i++) {
        for (int j = 0; j < board.getBoard()[i].length; j++) {
            this.add(board.getBoard()[i][j]);
        }
    }

    frame.add(this);
    frame.setSize(width, height);
    frame.setMinimumSize(this.minFrameSize);
    frame.setPreferredSize(new Dimension(this.width, this.height));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Does the GUI class JPanel intercept MouseClick events preventing the buttons from receiving the click event?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:53:09+00:00Added an answer on June 17, 2026 at 7:53 pm

    It does not fire because you don’t assign the listener to the button:

    public Tile(int type, int xCoord, int yCoord) {
        this.type = type;
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        addMouseListener(this); // add this line and it should work
    }
    

    However, if you just want to listen clicks, you should use ActionListener instead of MouseListener

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
I am trying build a DataTable one row at a time using the following
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Trying to build a project that uses managed C++/CLI and it compiles successfully when
I´m trying to build a Game with DirectX 11 on Windows 7. I have
I'm trying build a regex that will replace any characters not of the format:
Im trying to build a simple AJAX script that uses a form to send
Trying to build a dynamic gallery that is loaded via AJAX. I'm trying to
Im trying to build a form that calculates a total price based on a
Im trying to build a addin that can take some a selection of cells

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.