Luca Maiorano
Luca Maiorano

Reputation: 23

How to cancel a mouseclicker event in Java

I have a mouseclicker event added to some JLabels and, after one of them will be clicked, I want to remove the link between that JLabel and the mouseclicker event. To add the mouseclicker event to JLabel I use this code:

JLabel.addMouseListener(this);

There is a way to remove the JLabel from being clicked after the effect is solved? How can I do this? I searched something but I'm not sure to how I can describe the problem and search about it, so i didn't found results.

Upvotes: 2

Views: 2588

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

This may seem trivial, but you could simply do:

myLabel.removeMouseListener(this);

Option two is to leave the MouseListener in place, but make it smarter -- i.e., give it logic that allows it to ignore input if need be. This could be a simple if block such as

@Override
public void mousePressed(MouseEvent me) {
    if (someBoolean) {
        return;
    } 
    // here have your usual code
}

and then in your code, when you want to de-activate the MouseListener, simply change the listener's someBoolean field to false. This use of a boolean switch or flag is useful for when you need to turn the listener on and off repeatedly.

As a side note, you're usually better off not using this for your listeners as that is giving the main GUI class a bit too much responsibility. Instead use anonymous inner classes for simple few line code or named class for more involved listener code.

For example:

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.Border;

public class TurnListenerOnAndOff extends JPanel {
    private JLabel myLabel = new JLabel("My Label");
    private JCheckBox listenerEnabledCheckBox = new JCheckBox("Listener Enabled", true);

    public TurnListenerOnAndOff() {
        // make label bigger with a border
        Border outsideBorder = BorderFactory.createLineBorder(Color.black);
        Border insideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        myLabel.setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));

        // create and add MyMouseListener to my label
        myLabel.addMouseListener(new MyMouseListener());

        // add components to the GUI's main JPanel
        add(myLabel);
        add(listenerEnabledCheckBox);
    }

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            // if the JCheckBox isn't checked...
            if (!listenerEnabledCheckBox.isSelected()) {
                return;  // let's get out of here
            }

            // otherwise if the check box is checked, do following code
            System.out.println("myLabel pressed!");
        }
    }

    private static void createAndShowGui() {
        TurnListenerOnAndOff mainPanel = new TurnListenerOnAndOff();

        JFrame frame = new JFrame("On and Off");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

Upvotes: 4

Related Questions