1

If I've created a JFrame with 2 JButtons in it. Both buttons call another JFrame through actionPerformed(ActionEvent event) (so 3 JFrames in total, the main one and the two which are called from it).

I'll name the JFrame with the JButton's main, and the two called JFrame's frame1 and frame2.

I call frame1 from one of the JButton's in main. Then I press the second JButton which calls up frame2.

I want frame1 to be closed automatically when frame2 is called and vice-versa.

I've looked for a solution and not been able to find one but I'm hoping it's fairly simple.

Thanks, help is appreciated.

mre
  • 43,520
  • 33
  • 120
  • 170
DanMc
  • 848
  • 5
  • 15
  • 26

1 Answers1

5

I wouldn't recommend using multiple containers. Instead, use an appropriate layout manager (e.g. CardLayout). This way you'll have a single container with multiple views.

...deja vu...

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    *"...deja vu..."* "I've been to Bali too!" ;) – Andrew Thompson Apr 04 '12 at 13:15
  • 1
    Ah okay, I'm a comp sci fresher and haven't been taught that multiple JFrames is bad practice (for the record, I haven't been taught it was good practice either) so this is interesting. I notice that it warns me that using the CardLayout can be hard to do so it might be a bit above me, but I'll have a go. Thanks a lot :) – DanMc Apr 04 '12 at 13:19
  • @DanMc See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) (& chase the links) for ..much more on that matter. And my apologies for making such an obscure reference in my earlier comment. This site is about specific answers, not 'insider comments'. – Andrew Thompson Apr 04 '12 at 13:27
  • 1
    No worries, cheers for the response. I really don't mind the mick taken a little, I know it was a complete newbie question, just so long as I get a credible answer in the end I'm happy :p – DanMc Apr 04 '12 at 13:38
  • 1
    @DanMc I predict you will go far, and gain many allies in the process. :) – Andrew Thompson Apr 04 '12 at 13:39