So I have a JFrame which I’ve added a custom JPanel component.
The JPanel component has a button that I want to attach a listener to in my JFrame.
What is the best way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unless I’m reading this wrong, when you have added the JPanel yourself, you can just add an actionlistener to the button.
Or is it something else that you are asking here? e.g. if the custom JPanel is not developed by you. Then in that case, see if the panel exposes an API to add a listener to its buttons, if not then the last option is to iterate over its children to find the JButton: