Search code examples
formsuser-interfaceintellij-ideadesigner

Using IntelliJ IDEA UI designer, load a different form into a JPanel after clicking a button


I create a UI using IntelliJ IDEA UI Designer.

It has a JPanel with two buttons to the left. To the right there's a JPanel with a (nested) form, also created using the UI Designer.

What I would like to do is this:

when I click button 1 on the left, the panel on the right should show form 1

when I click button 2 on the left, the panel on the right should show form 2

I just want to create all kinds of forms using the UI Designer, and then show them whenever I want to.

This seems so basic but it's brought my development to a full stop. I'm wondering if I'm missing some kind of UI design concept?

I hope someone can tell me how I should do this.


Solution

  • Both form1 and form2 should have the outermost panel bound to some field of the corresponding class. Then, "the panel on the right" should also be bound to a field (of a third class).

    I'd set this latter panel's layout manager to CardLayout. Then I'd add both form1 and form2 to it with different keys. If we have all the panels bound to some fields, that's easy to do from code. Now one can switch between them in button action listeners. See CardLayout documentation for more details.