0

I have a problem. For example: We have MainFrame, on this form we have mainPanel. Next, creates new classes with names FirstPanel and SecondPanel wich extends JPanel. Now in MainFrame writes:

public MainFrame() {
        initComponents();
        setLocationRelativeTo(null);
        Main main = new Main();
        main.setMainPanel(mainPanel);
        mainPanel.setLayout( new BorderLayout() );
        mainPanel.add( PANELS_LIST.get(0) );
        mainPanel.validate();
}

PANELS_LIST - arrayList with all panels. PANELS_LIST.get(0) - jpanel with name FirstPanel . After this we will see FirstPanel. On the FirstPanel we have button. So, on this button click I want to change mainPanel state (need SecondPanel overwrites FirstPanel). How can i do this? Tried to send mainPanel like a param with creating setter or getter but doesn't work.. At all I need mainPanel to be accesable from any panel in any class

PS: ofcourse I can set mainPanel like a public and static, but.. I think this is bad solution

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gwalk
  • 211
  • 4
  • 13
  • 2
    Some approaches are examined int his possible [duplicate](http://stackoverflow.com/q/10523343/230513). – trashgod Nov 23 '14 at 16:08
  • 1
    *"FirstPanel and SecondPanel wich extends JPanel"* It is likely neither of those should extend `JPanel`. Instead they should just be instances of panel. Making them a separate class has caused this difficulty, and though it is possible to correct it while they are still extending panel, it makes little sense to do so. – Andrew Thompson Nov 23 '14 at 16:24
  • Why just an instance? There will be a lot of other functions. – Gwalk Nov 23 '14 at 16:27
  • 3
    Your question is very unclear; @AndrewThompson's point is that panels typically enclose view components; use `CardLayout` to change views; use property changes to communicate changes. – trashgod Nov 23 '14 at 16:38
  • create a static field in your MainFrame e.g. main and in the constructor of MainFrame class assign 'this' to main e.g. main = this; now you can access the main class anywhere by just using the MainFrame.main – Muhammad Nov 23 '14 at 16:49
  • Tip: Add @trashgod (or whoever, the `@` is important) to notify the person of a new comment. *"There will be a lot of other functions."* So? What methods of the extended panels are overridden? That is the ***only*** situation I can think of where it actually makes sense to override. Otherwise, these panels come into being mainly because of GUI editors. Are you using a GUI editor, or laying out the panels yourself? – Andrew Thompson Nov 23 '14 at 16:50
  • 1
    @Muhammad *"create a static field.."* ..and create more problems than you solve. – Andrew Thompson Nov 23 '14 at 16:51
  • Agree with @ Andrew, that @Muhammad's advice is not good advice since you'd be throwing out the baby with the bathwater. Making it static makes it hard to do unit testing, hard to enhance and upgrade. – Hovercraft Full Of Eels Nov 23 '14 at 18:24
  • @Andrew Thompson I'm using netbeans like a gui editor. And I will not use cardlayout because it isn't the thing i need – Gwalk Nov 23 '14 at 19:23
  • `"So, on this button click I want to change mainPanel state (need SecondPanel overwrites FirstPanel)."` -- this sure sounds like a perfect use-case for CardLayout use, and using the NetBeans GUI editor does not prevent one from using a CardLayout where needed. – Hovercraft Full Of Eels Nov 23 '14 at 21:12

0 Answers0