Link to home
Start Free TrialLog in
Avatar of fadl
fadl

asked on

When to call validate(),repaint() in my component

I'm writting my own component (JDK 1.1.3) by subclassing
Panel with one my Canvas extending Canvas and 2 Srcollbars in it + GridBagLayout. (I'm testing it on Solaris & Win95)
My Canvas implements paint() method - sure. That's not problem.

Everything goes well until some situations, where after drawing to my Canvas either one of the Scrollbars is mysteriously hidding or not entire myCanvas is beeing
redrawn? (Note that no resizes beeign occured and I
do repaint the whole myCanvas in paint() method -
it is not the fastest method, but it should work)

I know that repaint() call is just my ask for
paint() method to be called after some time.

Should I call one or more of the following methods ?
In which order and when ?

Component.
      doLayout()
      validate()
      invalidate()
      repaint()
Container.
      doLayout()
      validate()
      validateTree()

Can you explain me when and which ones should I call ?
Any URL with related reading is greatly welcome too.

Michal
Avatar of russgold
russgold

Since Canvas is not a Container, its doLayout() method is just a placeholder, and parent containers will not know to call it.  If you were overriding Panel, this would be a sensible thing to do.

Properly, you should call invalidate() when you change a component (especially its size) so that its parent will know to take action and will then call your validate() method for you.
Unfortunately, there is a known bug in JDK 1.1.3:

(See http://www.javasoft.com/products/jdk/1.1/bugs.html for a full list).

4059602 When a container is validating its children, it directly sets a component's valid instance variable, instead of calling its validate() method. This short-circuit prevents Component subclasses' validate() from being called.

Since you are *supposed to* do any recomputations here, this is rather annoying.  As a workaroud, you can maintain a separate valid flag internally and revalidate when paint() is called, if necessary.

One big problem you are having, though, is trying to place components on top of a Canvas. JDK does not guarantee that this will work and, as you have seen, it doesn't always.  Since JDK 1.1 supports a ScrollPane, why not use that instead?  It will make your work *much* easier.


Avatar of fadl

ASKER

Thank you for you answer.
I think I have to clarify my question.
I dont know exactly which methods to override in
MyCanvas extending Canvas

I overwrote paint() and after some change in the Component
I call repaint(). It works on 90%, as I described. What it
unclear to me is whether I should implement update() method too and/or to call invalidate() or getParent().validate()
or getParent().doLayout() and *when*.


ASKER CERTIFIED SOLUTION
Avatar of russgold
russgold

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fadl

ASKER

Thank you for your help. I switched to ScrollPane and
my code now works much better...

However, I still don't see any reason why you do not
recommend subclassing Canvas and painting to it your own
components - buttons with image faces, combo boxes, trees or
whatever.

Michal