Home > Enterprise >  MouseListener doesn't call mouseClicked method
MouseListener doesn't call mouseClicked method

Time:09-17

I thought I implemented MouseListener correctly, but when I click on the canvas it doesn't reach the line System.out.println("Click registered");

All the tutorials I've looked at implement a mouse listener the way I did.

The program compiles and everything works except for the mouse listener.

package ttt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Frame extends JFrame implements MouseListener
{
    public static Field field = new Field();
    public static Game game = new Game();
    public static TTTCanvas tttcanvas = new TTTCanvas();
    static final long serialVersionUID = 1l;
    private static final int width = 700;
    private static final int height = 700;
    
    public Frame() 
    {
        setLayout(new BorderLayout());
        setResizable(false);
        setBounds(400, 400, width, height);
        setTitle("Tic Tac Toe");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        add(tttcanvas);
        addMouseListener(this);
    }
    
    public void mouseClicked(MouseEvent e) 
    {  
        System.out.println("Click registered");
        int x = field.getFieldfromPixel(e.getX());
        int y = field.getFieldfromPixel(e.getY());
        game.updateField(x, y);
    }
    
    public void mouseEntered(MouseEvent e) {}  
    public void mouseExited(MouseEvent e) {}  
    public void mousePressed(MouseEvent e) {}  
    public void mouseReleased(MouseEvent e) {}
}

CodePudding user response:

Add the mouselistener to your ttt canvas

package ttt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame extends JFrame implements MouseListener
{
    public static Field field = new Field();
    public static Game game = new Game();
    public static TTTCanvas tttcanvas = new TTTCanvas();
    static final long serialVersionUID = 1l;
    private static final int width = 700;
    private static final int height = 700;
    
    public Frame() 
    {
        setLayout(new BorderLayout());
        setResizable(false);
        setBounds(400, 400, width, height);
        setTitle("Tic Tac Toe");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        add(tttcanvas);
        tttcanvas.addMouseListener(this); //changed here
    }
    
    public void mouseClicked(MouseEvent e) 
    {  
        System.out.println("Click registered");
        int x = field.getFieldfromPixel(e.getX());
        int y = field.getFieldfromPixel(e.getY());
        game.updateField(x, y);
    }
    
    public void mouseEntered(MouseEvent e) {}  
    public void mouseExited(MouseEvent e) {}  
    public void mousePressed(MouseEvent e) {}  
    public void mouseReleased(MouseEvent e) {}
}

CodePudding user response:

Works fine for me when I remove:

//add(tttcanvas);

A MouseEvent is only dispatched to a single component.

Your TTTCanvas class must also have a MouseListner, so it is receiving the event.

Not sure what your exact requirement is so I would guess you could either:

  1. add a second MouseListener to the TTTCanvas class or combine the logic of both listeners into one listener, or
  2. remove the listener from the TTTCanvas class

Some code to demonstrate my answer:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame extends JFrame implements MouseListener
{
//    public static Field field = new Field();
//    public static Game game = new Game();
//    public static TTTCanvas tttcanvas = new TTTCanvas();
//    static final long serialVersionUID = 1l;
    private static final int width = 700;
    private static final int height = 700;

    public Frame()
    {
        setLayout(new BorderLayout());
        setResizable(false);
        setBounds(400, 400, width, height);
        setTitle("Tic Tac Toe");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        //add(tttcanvas);

        JPanel panel = new JPanel();
        panel.setPreferredSize( new Dimension(700, 400) );
        panel.setBackground(Color.RED);
        panel.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                System.out.println("panel listener");
            }
        });

        add(panel, BorderLayout.PAGE_START);

        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e)
    {
        System.out.println("frame listener");
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> new Frame() );
    }
}
  1. if you click on the red panel you see "panel listener" because the panel receives the event not the frame.
  2. if you click on the grey you see "frame listener" because no other component has been added to the CENTER of the frames BorderLayout, so the frame receives the MouseEvent.
  • Related