Each time the JScrollPane must validate itself it looks for the size of its viewports child, your JPanel, which it uses to set its scrollbars. So your custom component must let the JScollPane know what size it is or wants to be shown with. One way to do this is to override the JComponent method getPreferredSize in your component and return the desired Dimension. The JScrollPane will ask for this during each validate episode.
Calling the JComponent method revalidate on an instance of MyPanel in your event code signals the JScrollPane to validate itself.