mouse event not triggering

Hi all, small question Quick response is required plzz. pardon if its a basic question, am new to programming and want to stick to good programming rule as much as possible.

Question:
i have a JFrame which contain a JScrollPane which contain a JPanel as viewportView.
i have implemented the JPanel with MouseListener and MouseMotionListener with the code shown below to chk if the event Fires but no response. (i also tried it on the JScrollPane)

where should i have put the MouseListener and MouseMotionListener ???
plz help, its urgent.

code to fire mouse action :




@Override
public void mouseClicked(MouseEvent event){
	System.out.println(event.getPoint());
	
	}//close mouseClicked	

@Override
	public void mousePressed(MouseEvent event){
		
		System.out.println(event.getPoint());
			}//close mouse pressed

@Override
public void mouseMoved(MouseEvent event){}//close mouseMoved

@Override
public void mouseDragged(MouseEvent event){}//close mouseDragged

@Override
public void mouseExited(MouseEvent event){}//close mouseDragged

@Override
public void mouseReleased(MouseEvent event){}//close mouseDragged

@Override
public void mouseEntered(MouseEvent event){}//close mouseDragged

code for initialisation :
P.S:
PuzzlePanel is a class which extends JPanel
PuzzleScrollPane is a class which extends JScrollPane

public class PuzzlePane extends JFrame{

	
	ImageData puzzleImageData;
	Dimension gameSetting = new Dimension();
	protected PuzzlePanel pp ;
	protected PuzzleScrollPane jsp ;
	protected Gallery gDialog ;
	protected Dimension dimDesktop ;
	public PuzzlePane(){
		
		initUI();
		
		
	}//close public declaration
	
	public void initUI(){
	
	 dimDesktop = Toolkit.getDefaultToolkit().getScreenSize();
	this.setSize(dimDesktop);
	this.setPreferredSize(this.getSize());
	pp = new PuzzlePanel();
	jsp = new PuzzleScrollPane(pp);
	
	jsp.setBorder(new BevelBorder(1));
	jsp.setBackground(Color.cyan);
	jsp.setOpaque(false);
	jsp.getViewport().setOpaque(false);
	jsp.setSize(new Dimension(500,500));
	/*jsp.getVerticalScrollBar().setValue(0);
	jsp.getHorizontalScrollBar().setValue(0);*/
	this.add(jsp);
	this.pack();
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setLocationRelativeTo(null);
	this.setVisible(true);
	}//close initUI
	

You should put the Mouse(Motion)Listener in the (constructor of) JPanel (PuzzlePanel in this case)

setMouse(Motion)Listener(Mouse(Motion)Listener listener);

u Rock ;D ;D ;D
thanks , the 2nd line made my eye spin for a few mins but i understood what u meant.
it seemed that when i implement MouseListener to the JPanel it doesnt ADD the listener by itself.
so i added an inner class which extends MouseAdapter (with all the event code), then set the listener in the constructor of the jPanel
it did the wrk :slight_smile: :slight_smile: :slight_smile:

Since you implemented the MouseListener to the JPanel itself, you could just have done this:
addMouseListener(this);

But I’m glad you were able to fix it :slight_smile: