Search code examples
javaswingmouseeventjlabelsetbackground

How to change JLabel's background color through mouse events?


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

Solution

  • 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(...);