Whoaa! Absolutely wrong answer!
The first absolute CARDINAL SIN you have done is to do all this in a non-EDT thread !!! There is no place to explain it ... There are only about 30 billion places on the Web to find out about it.
Once all this code is executed in Runnable in EDT (Thread Dispatch Thread), then:
You should not override preferredSize (although you can if you want) ... but you need to install it.
You absolutely must not set dimensions ( height and width , or setSize() ) directly!
What you need DO is to get java.awt.Container , panel in your example to "lay out itself" ... there is the Container.doLayout() method, but as the API documentation says:
Forces this container to lay out its components. Most programs should not call this method directly, but should call the verification method instead.
therefore the solution:
SimpleComponent comp = new SimpleComponent(10, 10, 100, 100); comp.setPreferredSize( new Dimension( 90, 90 ) ); panel.add(comp);
By the way, please use my experience: I spent hours and hours tearing my hair, trying to figure it all out validate, invalidate, paintComponent, paint , etc ... and I still feel like I just scratched the surface.
source share