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() {
        super("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();
                label[i][j].setOpaque(true);
                k++;
                if(k % 2 == 0)
                    label[i][j].setBackground(Color.BLACK);
                else
                    label[i][j].setBackground(Color.WHITE);
                label[i][j].addMouseListener(this);
                panel.add(label[i][j]);
            }
        }
        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);
        cb.setVisible(true);
    }
}

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

yea i dont know how to do it

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) {
 
    }

or

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

http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

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

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() {
                createAndShowGUI();
            }
        });
    }
    
    /**
     * 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");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Create and set up the content pane.
        JComponent newContentPane = new MouseEventDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public MouseEventDemo() {
        super(new GridLayout(0,1));
        blankArea = new BlankArea(Color.YELLOW);
        add(blankArea);
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 75));
        add(scrollPane);
        
        //Register for mouse events on blankArea and the panel.
        blankArea.addMouseListener(this);
        addMouseListener(this);
        setPreferredSize(new Dimension(450, 450));
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }
    
    void eventOutput(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                + e.getComponent().getClass().getName()
                + "." + NEWLINE);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
    
    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);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.