Reputation: 65
I have multiple panels nested inside other panels and I'd like to get a JPanel from there.
I've tried:
Component[] components = this.getComponents();
for both JFrame and a JPanel but it returns me only javax.swing.JRootPane
and not any JPanels
.
Is there another function I am not aware of that would return all JPanels inside JFrame or JPanel?
Upvotes: 1
Views: 1409
Reputation: 285405
You're accessing the JFrame's JRootPane, a component that holds a JLayeredPane, a component which holds the contentPane, a component which holds your JPanels:
I suggest that you not do what you're trying to do since your method of getting references is very brittle and will break if you change the structure of your GUI. Instead use fields to hold the key references that you need access to, be they JPanel fields or collection fields such as an ArrayList, or a field that references objects of your own classes.
Upvotes: 2