Efficiency of mouse listener on JButton

128 views Asked by At

Let say I created n JButtons and place them randomly in a JFrame and I add a mouse listener on each JButton.

My question is: When I click on any one of the JButtons, does Java check through all JButtons to determine which button am I clicking (worst case: n checks) OR it does not need to check through all buttons (worst case: 1 check)?

The reason I ask this is because I am trying to interact with 2DGraphics and I know one of the ways to check whether I click on a particular Shape is by checking through all Shapes whether that Shape contains my current mouse coordinates. So if I draw 10 shapes, my worst case would be 10. Honestly speaking, I feel that kind of inefficient.

So I was wondering how does JComponent handles an actionEvent. By checking through all JComponents or they don't?

If they don't, how does Java determines which button was clicked?

0

There are 0 answers