Wednesday, June 17, 2009

Non-drawing (J)Panels in Applets

I rewrote the Monitor applet, which displays a graph updated every second. This displays the performance of a particular machine in a convenient applet that can be wrapped up as a portlet and rearranged by the user on a webpage. Unfortunately I couldn't get it to draw. It worked, but the paintComponent calls came in with a 'device clip' only 10 pixels high, so preventing most of the graph from drawing. The reason was that I had failed to override the getPreferredSize() method for my subclassed JPanel. I only overrode getSize(), getHeight() and getWidth(). From memory getPreferredSize() is the one Swing uses as the canonical size of the panel. Once I copied the getSize() code into getPreferredSize() it all worked. Doh!

No comments:

Post a Comment