Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a JPanel contains a JButton

I have added a button to a JPanel. I want to remove the button if the JPanel contains the button. Is there any way to check whether the JPanel contains the button?

like image 768
JavaLearner Avatar asked Dec 03 '22 01:12

JavaLearner


1 Answers

If you have a reference to the JButton, call getParent(). If the parent is null, the button is not in the panel (or any container).

Alternately, do as @kleopatra suggested and call getComponents() on the JPanel instance and iterate the array looking for anything that is an instanceof JButton.

like image 131
Andrew Thompson Avatar answered Dec 15 '22 21:12

Andrew Thompson