I am constantly finding myself building programs where there are multiple screens. For example, consider a program where the initial layout offers two buttons: create file or edit file. Upon clicking one, it takes the user to a new screen supporting whatever button they press. Then they click a back button and it takes them back to the main screen of the program. I am wondering how to best do separate menus like this. Would it be best just to create separate methods setting up each screen, then call the appropriate one when a button (like “back” button) is clicked? This is what I was thinking of doing, but seeing as there are many ways to do this, I want to get opinions on a possibly better way of changing the screen displayed.
Thanks, AJ
I am constantly finding myself building programs where there are multiple screens. For example,
Share
Based on your 2nd comment to the original question, I think it is best to look at using Panels.
Look at using multiple panels for each of the activities you want:
Ensure all panels are hidden when initialised, and then simply swap a panel for another one upon a button click event using:
Depending on the layout you use, there are also other techniques. For example, rather than removing and adding (or showing and hiding multiple panels), if you use BorderLayout you can simple “replace” a BorderLayout area with another panel and then revalidate:
Note also that different Operating Systems (Windows, Mac etc) will have different styles they like to adhere to. For example you mentioned a typical Windows installer; people have come to expect an installer to look and work in a certain way, but on a different OS there are a completely different set of expectations and looks.
Further reading:
Tutorial on using panels
Java SE 7 (JPanel API)
Edit:
This comes down to personal preference. Do you want to initalise everything on startup and have quicker swaps between panels (or as you called them: “screens”), OR do you want a quicker inital startup and essentially have “lazy loading” of each component as and when you need it.
Personally I opt for everything during initalisation (unless there is a lot of things to do or load during your applications startup). This really comes down to personal preference.
Another edit:
Speaking about layouts, perhaps a different layout style would also help you out, something like this:
CardLayout tutorial
Hope this helps out somewhat.