javaswingjbuttonactionlistenercontentpane

When resetting the ContentPane, button function no longer works properly Java swing


I have the following actionevent on a button which takes me from the home screen to the second screen.

public void advancedBtnAction(ActionEvent evt, JFrame frame) {                                         

    frame.remove(this.getContentPane());
    frame.setContentPane(new advancedHome(frame).getContentPane());
    frame.revalidate();
    frame.pack();

}  

When I am on this screen I have the option to go back, this is the code for this button. This brings everything back that was on the home screen fine however when I click on the above button again it doesnt do anything?

public void BackButton(ActionEvent evt, JFrame frame) {                                         

    frame.remove(this.getContentPane());
    frame.setContentPane(new printerHome(this).getContentPane());
    frame.revalidate();
    frame.pack();
}

Could anyone help me with getting advancedBtnAction to work again after being bought back onto the content pane


Solution

  • You should be using a Card Layout for this type of requirement.