954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic GUI question

Hey guys

This is more of a question of how people generally do things.

Often in programs involving a GUI, I need to have more than one logical 'screen', for example if you were filling in a form, you would click "Next" and the next 'screen' would appear showing completely different components etc.

The way I used to solve this problem was to close the current JFrame and then initialise a whole new JFrame passing the information to the next frame. However this feels contrived and a bad solution to a problem.

How do people usually solve this problem? Do they have multiple JPanels added to one standard frame, and then switch between them?

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

You would typically have mutliple JFrames and chain from one to the other, BUT not pass all the info down the chain. Use the MVC pattern and keep all the info in a "model" class (or classes) that the GUI can call via standard get(...)/set(...) or add(...) methods. Each piece of the GUI can query the model for any info it needs, and send any new info to the model. When the user interaction is complete the model class(es) can the do whatever is required with it (save into database, compute results etc). This maintains a proper separation between the information/processing and the vaguaries of any particular GUI (eg JFrame vs smartphone screen vs web server...)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Ok, the chaining thing sounds fine, but do you initialise all of them at the start and then just use setVisible(boolean) to choose which one you would like to display at the current time?

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

I wouild initialise each only when it is needed - you'll probably want to populate one or two fields with data collected in earlier frames, and there may be frames that only appear when certain values are entered on earlier frames.
ps: Although I went for chaining, multiple panels in one frame is certainly viable, and someone ekse may have a different view on this. The inportant thing is to get the model separated from any particular GUI implementation - don't store the data in the GUI!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I agree with james its better to have more Jframes than manipulate by panels by setVisible() method and by writing a separate class for that data storing and retrival will make sure that it wont b lost when an error occurred in the gui

if u manipulate that by switching between panels. The panels u wont need would b unnecessarily using u r system resources and useless load to u r system unless u dispose them.

tizon
Newbie Poster
15 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: