BFreeman BFreeman - 5 months ago 37
Java Question

How do I dynamically add Panels to other panels at runtime in Java?

I'm trying to get into java again (it's been a few years). I never really did any GUI coding in java. I've been using Netbeans to get started with this.

When using winforms in C# at work I use a usercontrols to build parts of my UI and add them to forms dynamically.

I've been trying to use

like usercontrols in C#. I created a
form called
. This has a few simple controls on it. I am trying to add it to another panel at run time on a button event.

Here is the code that I thought would work:

mainPanel.add(new BlurbEditor());
//I've also tried all possible combinations of these too

This unfortunately is not working. What am I doing wrong?


I figured it out. The comments under the accepted answer here explain it: Dynamically added JTable not displaying

Basically I just added the following before the mainPanel.add()

mainPanel.setLayout(new java.awt.BorderLayout());