Add actionlistener to jpanel

I would like to know if there is a way to add an ActionListener to a JPanel? I have no problem adding them to JButtons, but JPanel does not seem to have such a method.

Basically, I have a bunch of JPanels inside a JFrame with a grid layout, and I would like to know if there is a way to find out when the user clicked on one of them.

Any help is much appreciated!

+6
source share
2 answers

An action listener is used to listen for action events. The button starts the action when it is pressed and released with the mouse (and not when pressed) or when pressed from the keyboard (space, mnemonics, keyboard shortcuts, etc.). This is a high level event. A mouse click is a lower level event that is handled by MouseListener .

Add MouseListener to your JPanel if you want to handle mouse clicks.

+14
source

Perhaps you can use AddMouseLitener. Stupid me ..

-1
source

Source: https://habr.com/ru/post/916777/


All Articles