Molehole
Molehole

Reputation: 65

Accessing a JPanel inside another JPanel

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

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

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:

enter image description here * Reference

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

Related Questions