Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add actionlistener to jpanel

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!

like image 706
YemSalat Avatar asked May 27 '12 07:05

YemSalat


People also ask

Can you add ActionListener to JPanel?

So you can't attach an ActionListener to a JPanel .


1 Answers

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.

like image 86
JB Nizet Avatar answered Oct 20 '22 07:10

JB Nizet