Solved

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

Posted on 1997-08-20
4
1,031 Views
Last Modified: 2008-03-10
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
Comment
Question by:fadl
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:russgold
Comment Utility
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
 
LVL 1

Author Comment

by:fadl
Comment Utility
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
 
LVL 4

Accepted Solution

by:
russgold earned 400 total points
Comment Utility
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
 
LVL 1

Author Comment

by:fadl
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now