0

I was wondering if there was a way to reuse a jframe without making it new. You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two). One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button.I forgot to mention that the first jtextarea contains a "back" jbutton, that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.

I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
breakstorm
  • 9
  • 1
  • 7

1 Answers1

5

You state/and I reply:

I was wondering if there was a way to reuse a jframe without making it new.

Yes, it is quite possible to re-use components (the generalization of your question).

You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two).

As has been stated, this is generally not a goood idea. Most Swing GUI applications use only one main window, the JFrame, and then either swap views such as with CardLayout or JTabbedPane, or show modal or non-modal dialog windows.

One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button. I forgot to mention that the first jtextarea contains a "back" jbutton,

It's most unusual for a JTextArea to have a button of any kind. Also, there is no such thing as a "passwordtxtarea", perhaps you mean JPasswordField? If so, please be precise with your terms when asking questions here. It's hard enough to guess what someone's program is like based on a description, that you don't want to make it harder on us. Also, it's very unusual to use a JTextArea for a user name field, since usually you'd use a JTextField. Again, precision really matters. Else we'll likely give you the wrong advice.

that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.

Yes, you should not be creating new components here but rather re-using previously created components. It's all do-able if you make your component a class field and if you make sure to create it only once. It's all how you code it.

I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?

Again it's possible. The solution will all depend on the structure of your program.

A word of advice: gear your GUI code toward making JPanels, not JFrames. This way you can place them anywhere they are needed -- in a JFrame, a JDialog, another JPanel, or swapped with a CardLayout,... anywhere. It greatly increases the flexibility of your program.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hey thanks :) But I already created all my JFrames, i'll keep that in mind for later on. And I think you misunderstood(or I MADE you misunderstand :/ that JTextArea has the button, i meant that the form had a JTextArea and a button ;) Thanks for the input, you seem to be a pro in this area ^^ I'm just a starter, so could you tell me, how would i change my component to a class field? (And creating it only once- does it mean calling it, or adding new in front of it? Because I also have to have it where it IS default when another jframe calls it, but not when it is between the two Jframes)Thanks^ – breakstorm Nov 03 '13 at 12:16
  • @user2945304: you need to show us code, especially how you're calling it. – Hovercraft Full Of Eels Nov 03 '13 at 12:21