I have a JLabel that receives an event, and would like to disable this 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();
}
}