Add actionlistener to jpanel

13.9k views Asked by At

I would like to know if there is any way to add an ActionListener to a JPanel? I have no problem adding those to JButtons, but JPanel seems not 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 any way to know when the user has clicked on one of them.

Any help much appreciated!

2

There are 2 answers

1
JB Nizet On BEST ANSWER

An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener.

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

0
YemSalat On

Aparently you can use AddMouseLitener.. Silly me..