Is this normal? How can i fix it?
Don't know if that is normal or not, but that is not the proper way to resize the panel. A Swing component is responsible to determining its size, not your application code. This is done by creating a setter method when you want to change a property of your component.
If you want to dynamically change the size of the panel then you should override the getPreferredSize()
method of your custom panel. Something like:
@Override
public Dimentsion getPreferredSize()
{
return new Dimension( new Dimension(300, (getCount() + 3) * 30) );
}
Then in you class you should create a setter (and getter) method for your count:
public void setCount(int count)
{
this.count = count;
revalidate()
repaint();
}
Now the revalidate will cause the preferred size to be recalculated and the scroll pane will then use the new preferred size.