javaswinguser-interfacenetbeans

How to change content in a Java GUI application without creating a new window each time


I need to make a Java desktop application for a client and the last time I did Java it was 2 years ago and a little odd.

My main query is regarding navigation between GUI. In the past, I would just create a new JForm (JFrame maybe?) whenever a button was pressed and a new GUI form/window would open up.

This time, I'd like the GUI to be inside one JForm/JFrame with just the inner content changing, how most applications look when you press a button.

I assume this is done by putting all of my GUI elements in JPanels, and deleting/creating them when buttons are pressed on the same JForm. Is that correct? If not, then how do I do it?

I'll also be using Netbeans GUI editor.


Solution

  • The simplest approach would be to use a CardLayout

    This will allow you to add multiple components to the UI and control which one is actively visible