0

I am new to Java Swing. I am trying to do research on how to change pages within the same JFrame (within the same window to be specific.)

What I am trying to do is that there are 2 pages.

The first page has a list of select-able contents and a button that says "Next." Hitting the "next" button would create a new page based on the selected content.

On the new page, there is a "back" button that goes back to the original page, and the original page still remembers the the choice of selected content.

How would I approach this? I have looked up CardLayout, but I am not sure if that is the right technique.

Originally, I thought creating 2 classes, each a JFrame. The first class (first page) upon hitting "Next" button would instantiate the second class (second page.) The second page would be on top of the first page in the same window, like a stack.

On the second page, there is "Back" button which would call this.dispose(), which would get rid of the second page (popping off the stack) but leave first page intact. The problem however is that creating the second page results in a new, separate window instead of being in the same window as first page.

Again, how would I approach this?

By the way, I am aware having multiple JFrames is a bad practice The Use of Multiple JFrames: Good or Bad Practice?

Community
  • 1
  • 1
Life
  • 41
  • 1
  • 6
  • 1
    Extend `CardLayout` to add the functionality of "go to previously displayed panel" by storing the previous panel somewhere after switching panels – Vince Feb 14 '15 at 17:27

1 Answers1

2

Yes Card Layout is the right option.

Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • This seems more like a comment than an answer, since it does not explain how to achieve the goal. `CardLayout` as it currently is doesn't support a "previous panel" – Vince Feb 14 '15 at 17:30
  • ha ha @VinceEmigh you are correct, i dont have anything more than that. – Ramesh Kotha Feb 14 '15 at 17:42
  • Thank you for pointing me in the right direction, Ramesh & Vince! – Life Feb 14 '15 at 17:44
  • Just wanted to check back in and say: I've done it with the cardlayout! Thank you! – Life Feb 14 '15 at 22:37