Hello everyone.

I have successfully added an image to my JPanel, but what I want to do is this: I want to be able to mark a section of the image as clickable. Then when the user clicks within that section, I run some action. So essential I want to give the feel of a button and action Listener. Any suggestions please?

Recommended Answers

All 3 Replies

Some information to add:

The Image I am using as the background image is larger than the JFrame it is in, I need it to be like that however. So the clickable area that I seek must stay locked to the picture and not the frame.

I'm still messing around with possible ways.

I have successfully added some invisible JPanel's to the areas. It seems that it is only by chance that these lined up. I fiddled for a bit, then it worked. Would it be advisable to then add some MouseListener's to these JPanel's?

And is there a better way to make areas in a picture clickable? Not a picture with pictures on it, just regions in a solid picture.

If you want to use graphics, then just use a Rectangle over the area that you want to be clickable. Then when you press the mouse button check the coordinates of the Mouse and it the Rectangle contains them. If they do then run the action. If the Image is in a JScrollPane this becomes more difficult.

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.