Skip to content
Advertisement

How to add multiple JComponents to a JPanel?

I’m trying to add multiple components to a JPanel, but only the most recently added component is displaying.

It seems like the problem is that the layout can only handle 1 component at a time, but I’m at a loss as to how to actually fix it. I’ve tried adding the components to a separate panel and then adding that to the main panel, and I’ve tried a few different layouts to no avail.

Here’s the relevant code, I’m currently trying just to get two TextRects, which extends JComponent, on the panel:

JavaScript

Any help would be appreciated.

Edit:

generateUML is called from an ActionListener in the controller when the user presses a “Generate UML” button:

JavaScript

Advertisement

Answer

Try this

JavaScript

If you set the BorderLayout, you should use its properties. Also, after calling revalidate(), you should call repaint();

Another option is a GridLayout

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement