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 7614145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:19:42+00:00 2026-05-31T02:19:42+00:00

Hi I am new to java and I thought I’d try produce a game

  • 0

Hi I am new to java and I thought I’d try produce a game where the user actually tries solving the 8 queens problem themselves. However, It increases in difficulty starting 8 rooks, up to 14 bishops then 8 queens.

I have created the chessboard successfully. I have a problem with my mouselistener… each square on the board is a button and when clicked my intention is that that square will change colour to indicate its been clicked, then all squares that cant be clicked on again will also change to indicate squares out of the game.

When the square is clicked it doesn’t seem to perform any action.
Sorry, I know its trivial.
Thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class rooks extends JFrame implements MouseListener{

    private final int BOARD_SIZE = 8;
    private final int BOARD_SIZE_COLS = 8;
    private final int BOARD_SIZE_ROWS = 8;
    // private JTextField bottom = new JTextField("")                                                   ");
    // private JLabel bannerl = new JLabel("The game");
    // private JButton queens = new JButton(" Play Queens ");
    private JButton rooks = new JButton(" Play Rooks ");
    // private JButton bishops = new JButton(" Play Knights ");
    private JButton[][] cboard = new JButton[BOARD_SIZE][BOARD_SIZE];
    private JTextArea bottomtextarea = new JTextArea();




    // constructor creating the chessboard
    public rooks(){
        this.setSize(500, 500);
        this.setTitle("rooks");
        // this.setIconImage();

        // create JPanels and add JComponents
        JPanel main = new JPanel(new BorderLayout());
        this.setContentPane(main);

        JPanel north = new JPanel();
        north.setLayout(new GridLayout(1,3));
        main.add(north, BorderLayout.NORTH);
        // north.add(queens);
        north.add(rooks);
        // north.add(bishops);

        JPanel south = new JPanel();
        main.add(south, BorderLayout.SOUTH);
        south.add(bottomtextarea);
        bottomtextarea.setEditable(false);
        bottomtextarea.setVisible(true);

        // create grid (actual chessboard) and initialise each button with no char
        JPanel chessBoard = new JPanel(new GridLayout(BOARD_SIZE, BOARD_SIZE));
        main.add(chessBoard, BorderLayout.CENTER);
        for (int i=0; i<BOARD_SIZE_ROWS; i++){
            for(int j=0; j<BOARD_SIZE_COLS; j++){
                cboard[i][j] = new JButton("");
                chessBoard.add(cboard[i][j]);

                // as it loops add colour to the board, if (i+j=even then white, otherwise black)
                 if ((i + j) % 2 == 0) {
                            cboard[i][j].setBackground(Color.black);
                        } 
                 else {
                            cboard[i][j].setBackground(Color.white);
                        }   
            }
        }

        cboard[7][7].addMouseListener(this);


        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        }

    public void mousePressed(MouseEvent e){

    }
    public void mouseReleased(MouseEvent e) {

     }

     public void mouseEntered(MouseEvent e) {

     }

     public void mouseExited(MouseEvent e) {

     }

     public void mouseClicked(MouseEvent e) {
        System.out.print("it has been clicked");
     }

     void saySomething(String eventDescription, MouseEvent e) {

    }





}
  • 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-05-31T02:19:43+00:00Added an answer on May 31, 2026 at 2:19 am

    You are adding a MouseListener to the last button only, the JButton at cboard[7][7].

    • Why use a MouseListener and not an ActionListener for JButtons? This makes no sense.
    • Why not add an ActionListener to all JButtons inside of the for loop?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a new Java web project I thought about using: Eclipse as IDE Mercurial
I am not new to java and C#. I thought I understand the concept
I've been struggled this java problem for some hours, which i first thought was
I'm about to join a new (Java) development team and a thought came to
I'm new to Java but I thought I understood the logging concepts. From what
I encountered an interesting problem today which I thought was not possible in Java.
As a C# developer new to Java, i thought it might be easiest if
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
We are starting a new Java EE project and am looking for suggestions regarding

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.