I’m a Java newbie learning Swing… Hopefully this is really simple.
I’ve created a JFrame and added a MenuBar and a Panel . . . When I press run, the window pops up showing the menu and the panel… So it works till here… now lets do something with it..
I have set the visibility of the Panel and MenuBar reference variables in MainFrame.java file as private..
The problem is, I want to access those variables… E.g: – I want to add content to the Panel and/or enable/disable things in the Menu(s).
But all that I am holding is a handle to the main JFrame.
How can I get a handle to the Menu or to the Panel?
I could add accessor methods to the MainFrame.java file . . . but I think Java might have some standard way of doing this. . . and if accessor methods were the typical way of handling this, I would assume they’d be provided by the NetBeans GUI Builder.
What am I missing?
Frank
The accessor methods is a good way to go.
Another way would be to do something like an inversion of control. Instead of getting references to your menu and panel objects from another classes, you could call methods in the main frame class to do the work on the menu and panel that you need to be done in the other classes.
Another choice would be to navigate through the component graph, but I would not recommend that because whenever you will add / remove components to your panels in the frame, you will have to change the logic that retrieves the references to the menu and panel that you need.