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?

Recommended Answers

All 4 Replies

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...)

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?

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.