user2935569
user2935569

Reputation: 347

Java removing and adding back MouseListener later

I have 4 JLabels.

upon first click:

I changed the background color to red and removed JLabel's MouseListener that I have clicked.

upon second click:

I change the background color to green, but the JLabel that I clicked previously shouldn't change from red to green since I had already removed the MouseListener.

upon third click:

I want to add back the MouseListener to the JLabel MouseListener that has been removed at first click and change the background color to black but I not sure how can I add back.

I tried to use addMouseListener method in my addbackMouseListener(JLabel label) but it's seems that I cannot pass in "this" in the parameter and I don't know what to pass in the parameter of the addMouseListener.

public void addbackMouseListener(JLabel label) {
    label.addMouseListener(this); <-- can't pass in this, what can I do?
}

Codes:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class rMouseListener extends JFrame {
    private JLabel[] jLabel = {new JLabel(), new JLabel(), new JLabel(), new JLabel()};;
    private JPanel panel = new JPanel(new FlowLayout());
    private int clickCount = 0;
    private boolean mouseRemoved = false;

    public rMouseListener() {

        for(int i = 0; i< 4; i++) {
            jLabel[i].setText(i + " ");
            panel.add(jLabel[i]);
            jLabel[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    JLabel label =(JLabel) e.getSource();
                    clickCount++;

                    /*remove the mouseListener for the label that is clicked*/
                    if(clickCount == 1) {
                        label.setBackground(Color.red);
                        label.setOpaque(true);
                        label.removeMouseListener(this);
                        mouseRemoved = true;
                    }

                    /* to verify that the mouseListener for that label is removed after
                    first click.*/
                    else if(clickCount == 2) {
                        label.setBackground(Color.green);
                        label.setOpaque(true);
                    }

                    /*check if the mouseListener is removed.
                      add back the mouseListener to the one that is perviously
                      removed at clickCount = 1 if it's removed*/
                    else if(clickCount == 3) {
                        if(mouseRemoved) {
                            addbackMouseListener(label);
                            label.setBackground(Color.black);
                        }
                    }


                }
            });
        }
        add(panel);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setSize(400,600);  
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void addbackMouseListener(JLabel label) {
        label.addMouseListener(this); <-- can't pass in this, what can I do?
    }

    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new rMouseListener();
            }
        });
    }
}

Upvotes: 0

Views: 832

Answers (2)

Sanjeev
Sanjeev

Reputation: 9946

Replace:

addbackMouseListener(label);

with

label.addMouseListener(this);

You do not need addBackMouseListener method.

Hope this helps

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347214

this refers to rMouseListener, which extends JFrame, it does not implement MouseListener

Your best bet might be to make your original MouseAdapter an inner class and create an instance of it when you create your class and simply add and remove it instead

For example...

public class rMouseListener extends JFrame {
    //...
    private MouseListener mouseListener;

    public rMouseListener() {

        mouseListener = new MouseHandler();
        for(int i = 0; i< 4; i++) {
            jLabel[i].setText(i + " ");
            panel.add(jLabel[i]);
            jLabel[i].addMouseListener(mouseListener);
        }
        //...
    }

    public void addbackMouseListener(JLabel label) {
        label.addMouseListener(mouseListener); 
    }

    //...

    public MouseHandler extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            JLabel label =(JLabel) e.getSource();
            clickCount++;

            /*remove the mouseListener for the label that is clicked*/
            if(clickCount == 1) {
                label.setBackground(Color.red);
                label.setOpaque(true);
                label.removeMouseListener(this);
                mouseRemoved = true;
            }

            /* to verify that the mouseListener for that label is removed after
            first click.*/
            else if(clickCount == 2) {
                label.setBackground(Color.green);
                label.setOpaque(true);
            }

            /*check if the mouseListener is removed.
              add back the mouseListener to the one that is perviously
              removed at clickCount = 1 if it's removed*/
            else if(clickCount == 3) {
                if(mouseRemoved) {
                    addbackMouseListener(label);
                    label.setBackground(Color.black);
                }
            }


        }
    }
}

Upvotes: 4

Related Questions