I've got a very silly question to ask.
I'm using NetBeans to built a small app and I'm having the following problem;
My main class is called mainApp
and is extending a JFrame
which in turn contains a JPanel
called drawingBoard
which I also extend for various (and off-topic) reasons..
The core problem is that at some point I need to access one of the fields of the mainApp
but due to the way NetBeans instantiates my main class..(as anonymous class) I can't get a reference to the container(that is my mainApp).
What can I do to get a reference of mainApp
and set the value of its field within drawingBoard
?
You can get a reference to the top level window by using Window win = SwingUtilities.getWindowAncestor(myComponent); and passing into the method call a refrence to any component that the top level window ultimately holds. If your main class is also your top level JFrame (you're not initializing any other JFrames), then you can cast the returned Window to your top level class type and call its public methods.
For example:
altered to be done by glowcoder's recommendation:
Another recommendation: since you're learning Swing, you're far better off not using NetBeans to generate Swing code for you but rather to code your Swing apps by hand. By doing this and studying the tutorials, you'll gain a far deeper and better understanding of just how Swing works, and it will help you should you need to use the NetBeans code generator to make anything more than the most simple of apps.