Hi there!
I'm new to Java and just arrived at this forum.
I hope I can get some help here - getting a little desperate.
Well, I have to develop a small game in Java and in this game the user must be able to click on different spots on the screen and, well, make things happen.
I have everything ready in my head but don't know how to make objects clickable and get info from them...

So, I've tried and tried many different things, have read lots of tutorials but so far no good.. :(

I was wondering if someone would let me know what to do and even if I'm in the right direction...
Here go the codes...
All this thing does is plot two squares on a panel. What I'm trying to do is make them clickable and get them to return some info like "my name is blah and i've just been clicked".

Thank you thank you a lot for any hints...

This would be the main Class

import java.awt.Color;
import javax.swing.JFrame;

public class EventTest
   public static void main( String args[] )
      JFrame frame = new JFrame( "Mouse Event Test" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      Panel painel = new Panel(); 
      frame.add( painel ); 
      frame.setSize( 400, 210 ); 
      frame.setVisible( true ); 

Other classes...

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Panel extends JPanel 
                            implements MouseListener {
    public void mousePressed(MouseEvent e) {
       saySomething("Mouse pressed", e);

    public void mouseReleased(MouseEvent e) {
       saySomething("Mouse released", e);

    public void mouseEntered(MouseEvent e) {
       saySomething("Mouse entered", e);

    public void mouseExited(MouseEvent e) {
       saySomething("Mouse exited", e);

    public void mouseClicked(MouseEvent e) {
       saySomething("Mouse clicked", e);

    void saySomething(String eventDescription, MouseEvent e) {
        System.out.println("Mouse event detected: "+e);
    Square q1 = new Square();
    Square q2 = new Square();
   public void paintComponent( Graphics g )
      super.paintComponent( g ); 
      this.setBackground( Color.WHITE );

    public void addMouseListener(MouseListener l) {


This one creates the small squares...

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

public class Square {

    public Square() {
    public void show(int x, int y,Graphics g) {
        g.setColor(Color.getHSBColor(125, 125, 125));
        g.drawRect(x, y, 20, 20);

I would name the Panel class something different since there is a Panel class in Java already.

What object exactly do you want to have a mouseListener attached to? A button on the panel? The Panel (again, possibly rename this class if it is not the Panel class from Java to avoid confusion) itself? I think you need a Panel () constructor within your Panel class. If it's the Panel itself that you want to listen to, I would add the line:


inside your Panel constructor. Delete this function:

public void addMouseListener(MouseListener l) {

It does nothing and, as far as I can tell, it is never called. Use Java's "addMouseListener" function, not your own.

Thanks for the quick reply...

I would name the Panel class something different since there is a Panel class in Java already.

Ok, you're right, I'll make sure to call it something else.

What object exactly do you want to have a mouseListener attached to? A button on the panel?

Actually I want attach a mouseListener to those two squares (the Square Class objects).
There will be a 15 x 15 (or bigger) matrix of those squares with letters in them. Once clicked, I want to get their x,y coordinates in the matrix.

If this is too weird, it will do if I can listen to the panel itself and get the mouse x,y coordinates (at least this way I can calculate the relative position and detect which letter the user clicked.

This idea of having each of the squares listening individually comes from Flash movieclips (they're so much easier to use) but this assignment has to be developed in Java.

I would really appreciate ideas and possibilities.
I'm just a beginner in Java and am having a bad time figuring out how to do it.

Thanks a lot!

Well, first things first. Don't worry about the Squares for now. I would get it so that your events show up first, which they will with a few minor changes. You need to have a MyPanel constructor (I changed the name), or at least you will need one before long, so might as well make one now. Again, remove that addMouseListener function. It is preventing Java's addMouseListener function from being called. For now the constructor can be as simple as this:

public MyPanel ()
	 addMouseListener (this);

Your mouseListener functions seem fine to me. Make this minor change (deleting the addMouseListener function and adding this constructor), see if you get any display when you click on the mouse and move it in and out of the panel, then go from there on the Squares.

(...) see if you get any display when you click on the mouse and move it in and out of the panel, then go from there on the Squares.

Thanks a lot! It works! :)

So you're saying I can do the same with each Square individually?
I've tried changing Squares code and including exactly the same from MyPanel, but nothing happened.
If it's not possible I'll do something to calculate the relative positions and call this solved.

Otherwise, if there is a way to make each Square listen to mouse events, I'd really prefer this approach, as I'd like to make it possible to create matrixes with any number of Squares based on the user's choice.

Great! Thanks again!

I think you have at least two choices on the Square aspect of the problem. One, within the mousePressed function, grab the coordinates of the mouse's location at the time of the mouse press. Create a function that checks whether those coordinates are inside of a square, and do something based on whether they are or not.

The getX and getY functions from the MouseEvent class will come in handy here:

A second option, I would think, is to have Square extend some Java Swing class like JButton that can be listened to by an actionListener (not a mouseListener). You'll add that to MyPanel, and add an actionListener to each Square within your MyPanel constructor. You'll add an actionListener to your MyPanel class and when either Square is pressed, that function will be called. getSource might come in handy if you go this route.

I like the first solution better than the second, but that's just personal preference.

Right now, as I do not have too much time ahead, I'll go for the first solution as it seems to be simpler to do (for me, at least).
Yes, I definitely have what I need to go ahead!
Thank you very much for your time and help.

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.