• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing handling multiple frames

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, up until now I've been writing programs with only 1 or 2 frames tops but now I think i'm ready to move on to slightly more complex things but whilst thinking about the best way to maintain and manage the code I've come across one thing that sticks in my head. What happens to a program that needs to handle multiple frames in a swing program? Do you create one class to manage all of the interfaces or do you create a new class every time you need to use a new frame/layout? Also, is it good practice to dispose of jframes and then create a new one?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple frames = Bad usability.

Recommended way is to have one application frame.
When you need to inform/question/ask confirmation from the user, use JDialog (via JOptionPane)
When you need to switch the contents conditionally, use a JPanel with the CardLayout
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Inspite of all this if you still feel the pressing need to use multiple frames, use internal frames
https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
 
Dave Tolls
Ranch Hand
Posts: 2284
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of the JFrame as your application window.
It might have multiple screens it can display, which are the JPanels, but there's still essentially a single main window.
Any additional small scale stuff is handled by dialogs.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:Also, is it good practice to dispose of jframes and then create a new one?


I would say it is generally acceptably to dispose JFrames and instantiate new ones. This could be less efficient than hiding them and reusing them, but I think only in rare circumstances would it make much difference.

BTW, I wouldn't unreservedly recommend the CardLayout approach. Sometimes it works well, sometimes not so much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!