2

It is to support image dragging. How to find if an image is clicked in JApplet?

I'm sure there is an easy way to do this, but I've looked everywhere and cannot seem to find it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ryley Matos
  • 137
  • 2
  • 2
  • 3
  • Hopefully this thread can help you in finding, [How to get Mouse Clicks](http://stackoverflow.com/a/10345165/1057230) on `Image` – nIcE cOw May 11 '12 at 02:49

2 Answers2

4

Options:

  • Use a JLabel to hold the image, and give it a MouseListener. Simple.
  • Or create a JButtton and use the Image as the button's ImageIcon. Probably simpler.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

See the Drag and Drop and Data Transfer lesson of the tutorial.

If the purpose of the dragging is to change the order in a slideshow or similar, look to a JList. See setDragEnabled(true) & How to Use Lists for more details.

For the display component, I would recommend a JLabel as suggested by @Hover. JList uses a JLabel as the rendering component by default.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433