1

I have a a JPanel with 9 JLabels with all of them setBackground() to the color of white and I have the mouse listener interface implemented in the class. My purpose is when i left click on my mouse in the region of a JLabel it will change the color to black or vice versa. This is my first time working with mouse events and I'm stuck. I don't seem to get the whole concept right. Can you guys help me out here? Thanks in advance! Oh by the way, I do understand that if I were to use a for-loop the process will be so much easier but I just want to find out whether we can do it the harder way. Cheers!

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

class TilePanelA extends JPanel implements MouseListener
{
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;
JLabel label7;
JLabel label8;
JLabel label9;


public TilePanelA() 
{
    this.setLayout(new GridLayout(3, 3));
    Dimension labelSize = new Dimension(300, 300);

    label1 = new JLabel();
    label2 = new JLabel();
    label3 = new JLabel();
    label4 = new JLabel();
    label5 = new JLabel();
    label6 = new JLabel();
    label7 = new JLabel();
    label8 = new JLabel();
    label9 = new JLabel();

    label1.setPreferredSize(labelSize);
    label2.setPreferredSize(labelSize);
    label3.setPreferredSize(labelSize);
    label4.setPreferredSize(labelSize);
    label5.setPreferredSize(labelSize);
    label6.setPreferredSize(labelSize);
    label7.setPreferredSize(labelSize);
    label8.setPreferredSize(labelSize);
    label9.setPreferredSize(labelSize);

    label1.setBackground(Color.WHITE);
    label2.setBackground(Color.WHITE);
    label3.setBackground(Color.WHITE);
    label4.setBackground(Color.WHITE);
    label5.setBackground(Color.WHITE);
    label6.setBackground(Color.WHITE);
    label7.setBackground(Color.WHITE);
    label8.setBackground(Color.WHITE);
    label9.setBackground(Color.WHITE);

    label1.setOpaque(true);
    label2.setOpaque(true);
    label3.setOpaque(true);
    label4.setOpaque(true);
    label5.setOpaque(true);
    label6.setOpaque(true);
    label7.setOpaque(true);
    label8.setOpaque(true);
    label9.setOpaque(true);

    this.add(label1);
    this.add(label2);
    this.add(label3);
    this.add(label4);
    this.add(label5);
    this.add(label6);
    this.add(label7);
    this.add(label8);
    this.add(label9);

    label1.addMouseListener(this);
    label2.addMouseListener(this);
    label3.addMouseListener(this);
    label4.addMouseListener(this);
    label5.addMouseListener(this);
    label6.addMouseListener(this);
    label7.addMouseListener(this);
    label8.addMouseListener(this);
    label9.addMouseListener(this);  
} // end of constructor

public void mousePressed(MouseEvent e)
{
    if (e.getSource() == Color.WHITE)
    {
        setBackground(Color.BLACK);
    } else
    {
        setBackground(Color.WHITE);
    }
}

public void mouseReleased(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}
}
Scorpiorian83
  • 469
  • 1
  • 4
  • 17
  • Take a look at this [EventObject reference](http://docs.oracle.com/javase/7/docs/api/java/util/EventObject.html#getSource%28%29) I have a feeling it's going to lead you to the cause of why your mouse event isn't working like you think. – Ryan J Jul 21 '14 at 19:43
  • 2
    I would also point out that you look at the difference between `mousePressed()` and `mouseClicked()`. If you want it to be when the mouse is pressed and released (as opposed to only when the button is pressed down), take a look at the latter method. – Ryan J Jul 21 '14 at 19:48

1 Answers1

3

e.getSource() will return the label that is clicked, not the color of that label (in your example, anyway)

So, cast to a label:

JLabel theLabel = (JLabel) e.getSource();

then setBackground on it appropriately:

if (theLabel.getBackground().equals(Color.WHITE)) theLabel.setBackgound(...);

splungebob
  • 5,357
  • 2
  • 22
  • 45
  • thanks so much mate one more thing is that I don't really quite understand when do we use == or .equals it's a bit confusing for me. Cheers! – Scorpiorian83 Jul 21 '14 at 19:54
  • 2
    `.equals()` is usually used when comparing equality with objects (content of the objects), such as `String`, and == being used for primitives or reference equality. This is not 100% accurate as written, but a general rule of thumb. See [this post](http://stackoverflow.com/questions/7520432/java-vs-equals-confusion) for more info – Ryan J Jul 21 '14 at 20:02