How to remove listeners according to component status?

Asked

Viewed 223 times

1

I have a Jlabel that receives an event, and would like to disable that event when the component is disabled (setEnable = false), and only activate it again when the component is enabled.

what I did:

import java.awt.Cursor;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class NovoClass extends JFrame {

    private JPanel painel = new JPanel();

    public NovoClass() {
        setSize(200, 150);
        ImageIcon image = new ImageIcon(getClass().getResource("/imagens/botãoExcluir.png"));
        JLabel imagelabel = new JLabel(image);
        JCheckBox check = new JCheckBox("Enable/Disable");

        imagelabel.addMouseListener(getMouseListener());
        imagelabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        painel.add(imagelabel);
        imagelabel.setEnabled(false);
        add(painel);
        painel.add(check);

        check.addItemListener((ItemEvent e) -> {
            if (check.isSelected() == true) {
                imagelabel.setEnabled(true);
                imagelabel.addMouseListener(getMouseListener());
            } else {
                imagelabel.setEnabled(false);
                //retirar evento.
            }
        });

        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public MouseListener getMouseListener() {

        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "Clicou aqui !");
            }
        };
    }

    public static void main(String[] args) {
        NovoClass novo = new NovoClass();
    }
}
  • What is the purpose of removing a system from a disabled component?

  • @Article here I did not want to extend much the code, more the component in some situations, opens Jdialogs, when disabled, I want to prevent the user to be able to click on the label, and open them.

  • It still doesn’t make sense. I think you’re visualizing it from the wrong side. Instead of removing and resetting event in component, why not check if component is disabled before event action?

  • @Once again that it is enabled, and win the event, if it is disabled later, the event will remain there, it will not ask it, so even if disabled, if it is clicked it will "run".

  • See the suggestion of my last comment. The approach suggested in it solves this. In your Mouselistener, check if the clicked component is active, if yes, do the action, if not, do nothing.

  • @Articuno I tried here, but it didn’t work, the only difference was the first time, as it gets disabled, then it worked the condition. However, after it is enabled 1 time, the event "continues there"

  • How are you doing?

  • using isEnable I see if the imageLabel ta habiltado, if it has it calls the method of the event.

Show 3 more comments

1 answer

2


A more interesting approach is to filter this directly into the Systener, rather than adding and removing it according to some component status.

To do this, just retrieve the clicked component and check if it is active, if yes, displays the alert, if not, do nothing. Your example would look like this (note in the method getMouseListener()):

import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class NovoClass extends JFrame {

    private JPanel painel = new JPanel();

    public NovoClass() {
        setSize(200, 150);
        ImageIcon image = new ImageIcon(getClass().getResource("/imagens/botãoExcluir.png"));
        JLabel imagelabel = new JLabel(image);
        JCheckBox check = new JCheckBox("Enable/Disable");

        imagelabel.addMouseListener(getMouseListener());
        imagelabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        painel.add(imagelabel);
        imagelabel.setEnabled(false);
        add(painel);
        painel.add(check);

        check.addItemListener((ItemEvent e) -> {
            if (check.isSelected() == true) {
                imagelabel.setEnabled(true);
            } else {
                imagelabel.setEnabled(false);
                //retirar evento.
            }
        });

        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public MouseListener getMouseListener() {

        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                JComponent comp = (JComponent) e.getSource();

                if(comp.isEnabled()){
                    JOptionPane.showMessageDialog(null, "Clicou aqui !");
                }
            }
        };
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(NovoClass::new);
    }
}

Proving that it really works:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.