To code pretty much makes a grid of a check board. However I want to modify it so if I click on any cell of the grid of the checkerboard it will display a red circle.

If I click on a cell that already has a circle, it will remove it

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

import javax.swing.*;

public class CheckerBoard extends JFrame implements MouseListener {

    JLabel[][] label;
    CheckerBoard() {
        JPanel panel = new JPanel(new GridLayout(8,8));
        label = new JLabel[8][8];
        for(int j = 0; j < 8; j++) {
            int k = j;
            for(int i = 0; i < 8; i++) {
                label[i][j] = new JLabel();
                if(k % 2 == 0)
        add(panel, BorderLayout.CENTER);
    public void mouseClicked(MouseEvent arg0) {
    public void mouseEntered(MouseEvent arg0) {
    public void mouseExited(MouseEvent arg0) {
    public void mousePressed(MouseEvent arg0) {
    public void mouseReleased(MouseEvent arg0) {
    public static void main(String[] arg) {
        CheckerBoard cb = new CheckerBoard();
        cb.setSize(475, 475);

However I want to modify it so if I click on any cell of the grid of the checkerboard it will display a red circle.

Do that then!

Have you tried? you posted the code you "want to modify" (so i assume you havn't written it) have you tried to modify it? How would you go about doing it?

People arn't just going to do you job for you, its not going to help anyone

Which method do you think might get called when the user presses a mouse button???

why dont you read about MouseListeners, or add a "System.out.println("the user has clicked");" line where you think it may get called and see what happens.

Just saying "i dont know how to do it" and not even giving the slightest indication of any ]effort whatsoever is not going to get you help. You clearly havn't tried to solve the problem yourself.

whatever ill just use some other site to help me. I already tried reading material relating to this, so that's why im asking for help!

you do that, but considering you didnt even bother to put anything into the

public void mouseClicked(MouseEvent arg0) {


public void mousePressed(MouseEvent arg0) {

methods that have already been written for you, even after reading about mouse listeners, perhaps the second result on google

if you can't be bothered to type "mouse listener" into google, there it is for you! If you cant even attempt at trying it, I doubt other sites will be ready with information. Is this a homework assignment? Because it looks a lot like it. If it is I advise you to quit the course, as you clearly cant be bothered with learning anything

Edited 6 Years Ago by hanvyj: n/a

this isn't homework, i found this piece of code online. I just want to modify it to what I wanted. I looked at similar code since it could be similar to how that mine game works. So i did look at methods like the mouse listener mouse events, panels etc. However, the code the people use are so long. I'm use to 1~3 separate classes since I'm self teaching myself the language from a book.

Anyway thanks for the help/info, and also the lack of encouragement

lack of encouragement! I tried so hard to get you to do anything!

public void mouseClicked(MouseEvent arg0) {
          System.out.println("look, i can be bothered");

There you go! What I was trying to hard to get you to do! Here is the "long" example java tutorial in my last post:

package events;

import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class MouseEventDemo extends JPanel
        implements MouseListener {
    BlankArea blankArea;
    JTextArea textArea;
    static final String NEWLINE = System.getProperty("line.separator");
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MouseEventDemo");
        //Create and set up the content pane.
        JComponent newContentPane = new MouseEventDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        //Display the window.
    public MouseEventDemo() {
        super(new GridLayout(0,1));
        blankArea = new BlankArea(Color.YELLOW);
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(200, 75));
        //Register for mouse events on blankArea and the panel.
        setPreferredSize(new Dimension(450, 450));
    void eventOutput(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                + e.getComponent().getClass().getName()
                + "." + NEWLINE);
    public void mousePressed(MouseEvent e) {
        eventOutput("Mouse pressed (# of clicks: "
                + e.getClickCount() + ")", e);
    public void mouseReleased(MouseEvent e) {
        eventOutput("Mouse released (# of clicks: "
                + e.getClickCount() + ")", e);
    public void mouseEntered(MouseEvent e) {
        eventOutput("Mouse entered", e);
    public void mouseExited(MouseEvent e) {
        eventOutput("Mouse exited", e);
    public void mouseClicked(MouseEvent e) {
        eventOutput("Mouse clicked (# of clicks: "
                + e.getClickCount() + ")", e);

Edited 6 Years Ago by hanvyj: n/a

This article has been dead for over six months. Start a new discussion instead.