954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Abstraction and MouseListener

I'm having some fundamental issues with some basic abstraction. Here is the super class:

public abstract class Tile extends JLabel implements MouseListener {

	public Tile() {
		setIcon(new ImageIcon(getClass().getResource("images/blank.gif")));
	}
	
	@Override
	public void mousePressed(MouseEvent e) { 
		System.out.println("clicked");
		
		switch(e.getModifiers()) {
		case InputEvent.BUTTON1_MASK: leftClick(); break;
		case InputEvent.BUTTON2_MASK: rightClick(); break;
		}
	}
	
	public abstract void leftClick();
	public abstract void rightClick();
}


sub class:

public class Mine extends Tile {
	
	@Override
	public void leftClick() {
		System.out.println("left clicked");
	}

	@Override
	public void rightClick() {
		System.out.println("right clicked");
	}

}


When I implement this on a JFrame, the image is visible, but the MouseListener is not triggering. I tried just using a generic JLabel and adding an inner MouseListener class, which works perfectly. For some reason, I can't get this to work using abstraction, however. Any ideas?


If I understand correctly, the below snippet is essentially the same thing as I implemented with abstraction.

JLabel label = new JLabel(new ImageIcon(getClass()
			.getResource("images/blank.gif")));
		label.addMouseListener(new MouseListener() {
			@Override
			public void mousePressed(MouseEvent e) { 
				System.out.println("click");
			}
		});
ztini
Posting Whiz in Training
299 posts since Jan 2011
Reputation Points: 54
Solved Threads: 52
 

disregard; I created an abstract class that implements ClickListener, then use addMouseListener() on my Tile super.

ztini
Posting Whiz in Training
299 posts since Jan 2011
Reputation Points: 54
Solved Threads: 52
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: