I've wrote an applet that renders some vector data. Applet's content pane has a JPanel
child as the canvas. Before the first rendering and (paint()-ing) this JPanel needs to
know its' own size to properly caclulate the size of object to be drawn. JApplet has a
BorderLayout, so everithing gets the size automatically.
First access of the applet is working fine. After a stop() and destroy() event (but
without closing the browser) and hitting again the applet's page the applet crashes because
of JPanel.getWidth() and getHeight() returns 0.
( I'm doing everything _after_ the init() and start() using invokeLater() ).
So I'm confused a bit, how and when the layouted components get their size.
After the first paint()? But then how can I calculate the correct sizes of object to be
drawn? And why does work the applet after the first loading?
(Just a notice: none of my browsers follow the standard of applet's lifecycle events.
Displaying applet's page always causes init() and start(), and moving to another page
always causes stop() and destroy(). And I can't produce a combination of mouse clicks
that causes a start() without init() or a stop() without destroy().)
Thanks for help,
regards,
Feri
hiwa - 22 Oct 2006 10:03 GMT
> I've wrote an applet that renders some vector data. Applet's content pane has a JPanel
> child as the canvas. Before the first rendering and (paint()-ing) this JPanel needs to
[quoted text clipped - 18 lines]
> regards,
> Feri
getWidth(), getHeight() and getSize() et al returns non-zero values
after the component become visible. Often used idiom is to call
them in paint() or paintComponent() method of custom component.
In order to force particular size, use setPreferredSize and/or
override the getPreferredSize() method. Both methods works only
for Swing components.
Zaka Ferenc - 22 Oct 2006 11:16 GMT