Solved

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

Posted on 1997-08-20
4
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 98
even odd program using while loop 3 73
JDBC performance 8 29
Given a github repo find email address of all collaborators in that repo 1 32
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

756 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