0

I have the following hierarchy with Swing:

JFrame
  |
  +---> several JPanes
        one special JPane with a Canvas
         |
         +------> Many Shapes on the Canvas that are derived from JComponents

I am confused where to put the Mouse event handlers to detect whether one of the shapes has been clicked. All examples I found so far, attach the event handlers to the root JPane, but since I want to process the events on the circles themselves, it feels quite expensive to loop over all shapes, when a click event on a JPanes is detected.

What is the right approach to have "local" events on the Shapes?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
poseid
  • 6,986
  • 10
  • 48
  • 78
  • Best choice is probably to attach the listener to the source component you are most interested in. MouseListeners block those components below, just so your aware... – MadProgrammer Jun 05 '14 at 10:17
  • Add the listener to the custom `JComponent`(s). – Andrew Thompson Jun 05 '14 at 10:17
  • 1
    *"it feels quite expensive to loop over all shapes"* It would only become relevant if there were *thousands* of them. – Andrew Thompson Jun 05 '14 at 10:19
  • 1
    @AndrewThompson is correct; the example cited [here](http://stackoverflow.com/a/11944233/230513) scales well into the thousands; beyond that use the flyweight pattern, e.g. [tag:jgraph]. – trashgod Jun 05 '14 at 14:00

0 Answers0