I need to run a method (lets call it SampleMethod) in a panel class upon the click on specific button. As panels are added to some sort of list in the main window class instance, I just made it public, and want to access it in mouseClicked event of the button.
So, how do I get the main window instance, so I could get that panel list, get the specific panel and run SampleMethod with this instance?
I need to run a method (lets call it SampleMethod ) in a panel
Share
The main window class instance? It seems you have only one instance of your main window which may extend
JFrameorJPanelorJDialogetc. Anyway, you can use Singleton design pattern in your application to get and use the same instance (in your case the same main instance) anywhere in your code. In Singleton pattern, the constructor of the class that we apply the pattern is private, so you cannot make instances of it. However this class has also its single static instance which can be referenced via getter method and used anywere in your code. Take a look at Singleton examples on the web and you will be able to make a call likeYourJFrame.getInstance().getPanels(10).sampleMethod();in your nested classes and methods, in your case
mouseClicked().