[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

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
0
fadl
Asked:
fadl
  • 2
  • 2
1 Solution
 
russgoldCommented:
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.


0
 
fadlAuthor Commented:
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*.


0
 
russgoldCommented:
The default behavior of update() is to call paint() after clearing the background - this allows paint to only be concerned with drawing foreground information.  It is frequently overridden (to *just* call paint()) to avoid flicker - in such a case, the repaint method is responsible for refreshing the relevant parts of the background, generally by simply copying on offscreen image.

In your case, it sounds as though you are relying on components (scrollbars) being repainted may clear them whenever a section of the canvas which does not intersect the scrollbars needs to be redrawn (since update clears the *entire* background).  Overriding update() can fix this, as long as you take care to refresh the background yourself.

You should call invalidate() only when your structure is modified (size changed, for example) - but in that case, it will be called automatically for you.

getParent.validate() should be unnecessary and, as I noted above, it is broken anyway, so it won't help.

There is no need to call getParent. doLayout().  The AWT will take care of that.

Your choices appear to be:
1. Use a ScrollPane  - the easier choice
2. override update() and ensure that paint() cleans up the background.

If this is not enough, you may need to post a section of your code for more specific help.

0
 
fadlAuthor Commented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now