Member Avatar for ztini

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");
			}
		});
Member Avatar for ztini

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.