0

I'm making a chess-like game in java and I'm having an issue with the click events. The mouseClicked function isn't responding to my clicks on the window and for no apparent reason.

I have already tried a few things such as changing class names and using different functions but nothing has worked.

package main.game.com;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;     

public class ClickEvent extends MouseAdapter {

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

}

package main.game.com;

import java.awt.Canvas;

public class Main extends Canvas {

    private static final long serialVersionUID = 1673528055664762143L;

    private static final int WIDTH = 416, HEIGHT = 439;

    public Main() {
        Window window = new Window(WIDTH, HEIGHT, "DARRAGH", this);
        this.addMouseListener(new ClickEvent());
    }   

package main.game.com;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window extends Canvas {

    private static final long serialVersionUID = 6733885629776844621L;

    public Window(int width, int height, String title, Main main) {

        JFrame frame = new JFrame(title);   

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(main);
        frame.setVisible(true);
        main.start();

    }
}

The first set of code is my mouseAdapter library and the second is the first part of my main class containing the clickListener.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Procode238
  • 43
  • 7

1 Answers1

0

You're creating a Handler object, good, you're adding it to a Canvas object (the this -- why Canvas?), and you're creating a "top-level" window object which is in fact of Window type, but you never add the Canvas to the Window, nor do you appear to display the Window, so there is no reason to expect this code to in fact work.

Now, I'm guessing that there is more code out there that you're not showing us, and this may have relevance, and if so, do consider creating and posting an adequate MCVE for giving us the best understanding of your problem.

OK, I created a MCVE with your code:

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

public class Main extends Canvas {

    private static final long serialVersionUID = 1673528055664762143L;

    private static final int WIDTH = 416, HEIGHT = 439;

    public Main() {
        Procode238Window window = new Procode238Window(WIDTH, HEIGHT, "DARRAGH", this);
        this.addMouseListener(new ClickEvent());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new Main();
        });
    }

}

class ClickEvent extends MouseAdapter {

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

}

// renamed to avoid clashing with the java.awt.Window class    
class Procode238Window extends Canvas {

    private static final long serialVersionUID = 6733885629776844621L;

    public Procode238Window(int width, int height, String title, Main main) {

        JFrame frame = new JFrame(title);

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(main);
        frame.setVisible(true);
        // !! main.start();  // this method doesn't exist
    }
}

and it works

Note that:

  • This whole code can be copied and pasted into a single IDE file and run and has the necessary main method, both needed for it to be an MCVE
  • I've renamed the Window class to avoid a naming clash and confusing with the java.awt.Window class
  • Your code calls the Main class's .start() method, a method not shown. Could this be causing problems?
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Procode238: since you don't know where the problem is or why it is occurring it is not wise to assume what is necessary and what is not. Best (again) to post a [mcve] *always* when asking about code that is not behaving properly. Please read/study this important link about a very useful tool. – Hovercraft Full Of Eels Feb 14 '19 at 22:31
  • @Procode238: please read the [mcve](https://stackoverflow.com/help/mcve) link before posting more code as it will help both you and us. – Hovercraft Full Of Eels Feb 14 '19 at 22:32
  • @Procode238: again, please comment back when you've posted your MCVE. In the mean time, please check out my [answer here](https://stackoverflow.com/a/21067135/522444) to a somewhat similar case. – Hovercraft Full Of Eels Feb 14 '19 at 22:42
  • thank you for your replys. Hopefully the MCVE problem should be resolved as I have posted what I understand to be an MCVE. Once again thank you and I look forward to any of your future replys. – Procode238 Feb 14 '19 at 22:54
  • @Procode238: I created a valid MCVE with your code (thanks for the update), including a necessary `public static void main(...)` method, and it **works**. Please post a MCVE that reproduces your problem. Could there be an issue in the `.start()` method that you call but that you don't show us? – Hovercraft Full Of Eels Feb 14 '19 at 23:00
  • Thanks Hovercraft Full Of Eels you're a living legend. A wish I could thank you more. It wasn't the start message (sorry about that). it must have been that extra line of code you added in the main method. Once again thanks so much. – Procode238 Feb 14 '19 at 23:08
  • 1
    You mean that you didn't have a main method? – Hovercraft Full Of Eels Feb 14 '19 at 23:14
  • no you put a SwingUtilities.invokeLater thingamobober which I don't know what does yet, but I hope to find out soon! – Procode238 Feb 14 '19 at 23:17