1.11M Members

removeMouseListener doesn't work?

 
0
 

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?

 
0
 

Here's a sample of what I mean if someone actually wants to compile it so they can see it visually.....

Left then Right click the JButton.. It will say "Left Click" then "Right Click" even though it should have never reached the point where it should say "Right Click" because the MouseListener should have been removed...

Try Right clicking then Left clicking... Now it works, because the ActionListener was removed correctly......

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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


public class test extends JFrame {
	
	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run(){
				new test();
			}
		});
	}

	private test(){
		super("test");
		launch();
	}
	
	private void launch(){
		setBounds(100, 100, 100,75);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
                
                JButton b = new JButton("Test Me");
		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());
				}
			}
		});
		
		JPanel p = new JPanel();
		p.add(b);
		setContentPane(p);
		setVisible(true);
	}


	private void performLeftClickAction(JButton b){
		b.setText("Left Clicked");
		//problem starts here
		b.removeMouseListener(b.getMouseListeners()[0]);
		b.removeActionListener(b.getActionListeners()[0]);
	}
	private void performRightClickAction(JButton b){
		b.setText("Right Clicked");
		//error here
		b.removeMouseListener(b.getMouseListeners()[0]);
		b.removeActionListener(b.getActionListeners()[0]);
	}
}

What's going on here.... Am I using lines 61 and 67 incorrectly:
b.removeMouseListener(b.getMouseListeners()[0]);
to remove the MouseListener cause it's not working....

Removing the ActionListener does work though...

 
0
 

Red marked parts are changes that I made, I hope it doesn't need explanation

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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


public class Test extends JFrame {
	public boolean mouseClick = false;
	
	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run(){
				new Test();
			}
		});
	}

	private Test(){
		super("test");
		launch();
	}
	
	private void launch(){
		setBounds(100, 100, 100,75);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
                
                JButton b = new JButton("Test Me");
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae){
				if(!mouseClick)
				{				
					performLeftClickAction((JButton)ae.getSource());
				}
			}
		});
		b.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent me){
				if(SwingUtilities.isRightMouseButton(me)){
					if(!mouseClick)
					{				
						performRightClickAction((JButton)me.getSource());
					}				
				}
			}
		});
		
		JPanel p = new JPanel();
		p.add(b);
		setContentPane(p);
		setVisible(true);
	}


	private void performLeftClickAction(JButton b)
	{
		b.setText("Left Clicked");
		//problem starts here
		b.removeMouseListener(b.getMouseListeners()[0]);
		b.removeActionListener(b.getActionListeners()[0]);
		mouseClick = true;
	}
	private void performRightClickAction(JButton b){
		b.setText("Right Clicked");
		//error here
		b.removeMouseListener(b.getMouseListeners()[0]);
		b.removeActionListener(b.getActionListeners()[0]);
		mouseClick = true;
	}
}
 
0
 

Your button already has a mouse listener installed before you add your own. The button starts with a BasicButtonListener by default. Your action listener is removing that listener, but leaving your own mouse listener in place.

 
0
 

so when I did b.removeMouseListener(b.getMouseListeners()[0]);

What did it remove exactly? I'm sure it removed something because it didn't give me an arrayoutofbounds exception which it would have if it was a an array of size 0.... b.getMouseListeners() returns an array of size 0 if there are no listeners attached to b right?

 
0
 

After you added your mouse listener, the mouseListeners collection contained 2 listeners: The original BasicButtonListener, and the one that you added. That code b.removeMouseListener(b.getMouseListeners()[0]); removed the BasicButtonListener because it was at index 0, leaving yours still in place.

 
0
 

great.. I tested it and it works... Thank you everyone for the help!

I didnt know about the BasicMouseListener..

b.removeMouseListener(b.getMouseListeners()[0]);

is reserved for the BasicMouseListener. Anything after that, will be on index 1 and on.. I thought I tested it before, but I guess not :)

thanks again.

Question Answered as of 6 Years Ago by Ezzaral and peter_budo
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article