I am designing a bunch of JPanels, so to make it a bit easier for myself I've made a CustomPanel class that extends JPanel. This class contains a main method that sets up a JFrame with the JPanel inside. All the JPanels I design then extend CustomPanel, this way I only need the main method of each Panel to call the main of the CustomPanel class, rather than having to build a JFrame in each new Panel class. Let's say that I'd have a class TestPanel that extends CustomPanel.
The problem arises when I need to actually add a Panel to the JFrame when I call the main method in the corresponding Panel class.
I want to add a way to the CustomPanel class of getting an object of the Panel class from which I'm calling the main method. So for example, adding a TestPanel object. But because the main method is static, any method used for this would have to be static too. This means I can't override that method in each Panel class to return an object of that Panel. And Method hiding doesn't work either, as I would call the static method on the CustomPanel class which has to return null, or a Custom Panel, thus adding my CustomPanel or null to my JFrame.
I'm struggling to see any way of overcoming this obstacle, which is why I'm hoping someone will have some suggestions/implementations for how to do this. I have deliberately chosen not to post any code, as it would just be empty classes, and I'm not even sure if a static method is the way to go, or if there are other ways of achieving this.