If you have a reference to JButton , call getParent () . If the parent is null , the button is not in the panel (or in any container).
Alternatively, do as @kleopatra suggested, and call getComponents () on the JPanel instance and iterate through the array, looking for everything that instanceof JButton .
source share