JScrollPane uses the preferred size of its viewport view, not the "actual" size. Your panel has a preferred size of 0x0 as it has no layout manager to automatically calculate it. If you really want to continue using a null layout manager (which I do not recommend), then use setPreferredSize to set it explicitly.
There are also three problems with your paintComponent method:
1) It's public while it should remain protected.
2) The first call should be super.paintComponent(g); but that's missing.
3) You're modifying the frame (removing a component, repositioning that component, adding it again) from the method. You really, really shouldn't. First of all, the adding and removing is completely unnecessary if you only need to set the bounds. This should be done from the actionPerformed method itself.
After a quick glance, apart from still setting the bounds in the paintComponent method I don't see much wrong. You do have several calls to setSize in your actionPerformed method, of which only the very last one will be effective; the ones before that will be caused to be discarded because of the last one.
Rob Spoor wrote:After a quick glance, apart from still setting the bounds in the paintComponent method I don't see much wrong. You do have several calls to setSize in your actionPerformed method, of which only the very last one will be effective; the ones before that will be caused to be discarded because of the last one.
I have solved the problem. Your suggession was useful. Thanks for that. I used setsize multiple times to refresh the frame. Because if i didn't write it I need to do it manually by resizing window.