Solved

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

Posted on 1997-08-20
4
1,035 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
ID: 1225428
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
ID: 1225429
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
ID: 1225430
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
ID: 1225431
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 77
Systems talking to each other 5 123
simple java question 3 44
Java Loop 6 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

932 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

9 Experts available now in Live!

Get 1:1 Help Now