I assume that you added the JPanel
extension to another component (for example, JFrame
, JApplet
, another JPanel
, etc.). You mentioned that you have a mixture between the need to add a panel to the subcomponents inside this panel and the โother objectsโ that the panels need to listen to. It would probably be better to add a panel to these "other objects" next to where you add the JPanel
extension to its spanning JFrame
or other parent component outside the definition of your extension class.
However, for the subcomponents of the panel that your panel should listen to, I think that everything you do is fine if these subcomponents are not visible to objects outside of your JPanel
extension class definition. A warning simply indicates that what you are doing may be unsafe, but ultimately, when your panel receives garbage collection, like all subcomponents that it owns, including any listener lists, they support this point back to the JPanel
extension. Because of this, I think that calling add*Listener(this)
using the private method of your JPanel
extension and calling it from your constructor is fine.
Another option is to use Eclipse so that you no longer receive these warnings ... (completely joke;).
source share