J. Doe
J. Doe

Reputation: 33

how to not show objects from last screen when "switching screens?"

I'm newish to programming so there might be something obvious I don't know of that's a solution to this.

I'm currently writing a program that tests the user on words that they've entered before. When the user is entering words, the screen shows a jtextfield and jtextarea among other things for adding a word and it's definition and a variable is set to the constant NOT_TESTING to remember that that's the screen being displayed. My question is, when I want to switch screens to show the testing screen which has a jlabel displaying the word and a different jtextfield to write the definition of a word, what's the best way to not show the jtextfield and jtextarea from the screen where the user submits a word? (I'm not sure screen is the proper term, but I'm using it anyway.) If I don't make the components instnace variables, then I can't remove them when making a different screen, but it seems ridiculous to make them instance variables and then remove them whenever I switch screens, create new objects when going back to the old screen, and have so many instance variables.

Sorry for the long explanation, basically my question is: what's the best way to not show objects from previous screens when drawing a new screen?

Upvotes: 0

Views: 46

Answers (1)

saagarjha
saagarjha

Reputation: 2320

Take a look at CardLayout, which allows multiple JPanels to be shown without the other one showing (basically, it makes the entire window that panel and you can switch between them).

Upvotes: 2

Related Questions