Skip to content
Advertisement

Container not displaying in JScrollPane

I have a JScrollPane that will fill up with buttons added by the user. Currently, the user creates a new button and it is added to the container that is inside the scroll pane but nothing is displayed.

Is this because the scroll pane has already been displayed?

Initiating the scroll pane and container:

JavaScript

Adding new JButton components to the container:

JavaScript

Advertisement

Answer

JavaScript

Don’t add components to a JScrollPane. The component needs to be added to the viewport of the scollpane. This can be done in one of two ways:

JavaScript

or

JavaScript

I would use the first way unless you dynamically change the component in the viewport.

Then when you add a component to the visible gui the code would be:

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