I have a JButton with both an ActionListener and a MouseListener:
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
performLeftClickAction((JButton)ae.getSource());
}
});
b.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent me){
if(SwingUtilities.isRightMouseButton(me)){
performRightClickAction((JButton)me.getSource());
}
}
});
Here are the 2 actions for right clicking and left clicking:
performLeftClickAction(JButton b){
//problem starts here
b.removeMouseListener(b.getMouseListeners()[0]);
b.removeActionListener(b.getActionListeners()[0]);
}
private void performRightClickAction(JButton b){
//null pointer error here
b.removeActionListener(b.getActionListeners()[0]);
}
When I left click then right click, I get a null pointer error, but I should never have reached performRightClickAction() if the MouseListener was removed in performLeftClickAction()...
At least we know the ActionListener gets removed, otherwise I wouldn't be getting the null pointer error when I try to remove it the 2nd time...
I must be doing something wrong.... How can my MouseListener stay, but my ActionListener remove if I'm removing them the same way?