Skip to content
Advertisement

Swing: can’t paint rectangle

I have this simple code: the JFrame adds custom Jpanel MyPanel which overrides paintComponent method to draw the rectangle. However nothing shows up on the window:

JavaScript

Advertisement

Answer

By default, an empty JPanel has a size of 0x0, so when you add it to your UI, the layout manager (in this case) honours it’s request and makes it 0x0

Start by adding …

JavaScript

to MyPanel

Also, the repaint and revalidate calls aren’t going to do anything, as the window isn’t yet attached to native peer (rendering surface)

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