Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is paintComponent( ) called everytime we change the focus ?  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read this in a book..
"
The argument to the method paintComponent( ) (i.e; a Graphics object) is the actual drawing canvas that gets slapped onto the real display. You can't get this by yourself; it must be handled to you by the system. You can ask the sysytem to refresh the display ( repaint() ), which ultimately leads to paintComponent() being called.
"
So, i don't understand why is there a need to call the paintComponent( ) everytime? liike it's called when you minimize/maximize the frame.. etc.
this is not the case with widgets like buttons, checkboxes, textareas... many more.
then why is this the case with paintComponent( ) ??
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what is meant by saying that..
"You can't get the object by itself" ???
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book?

Imagine you have a frame. You move it. Now the OS “tells” the JVM it isn't painted correctly any more; it needs to be painted again with all its pixels in different locations.
You bring something else to the front and the OS “tells” the JVM it isn't painted correctly any more; it only needs part of itself painted.
In those instances, and many many more, the JVM sends a message to the frame and calls repaint (not paintComponent) on all its components. Then repaint calls paintComponent.
You never call paintComponent yourself, leaving that to the repaint method. You are allowed to call repaint yourself, but that is unusual. If things work OK, you leave all the painting calls to the JVM and forget all about them.

The repaint method finds the Graphics object which belongs to the current GUI and that has all the details of the frame, so it can work out size, shape, etc.

Please give more context about getting the object itself; I don't understand that.

Transferring discussion to our GUIs forum.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, i got my doubt cleared.
But what's hassling me now is that, why you need to paint the frame (panel) again when focus changes?
there is no such need in case of non-graphics stuffs...like we never make our whole say, a hello world program , to execute again on focus change... so why is there a need in case of GUI-based stuffs ??
btw,
i read it in the book... Head First Java.
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which page in HFJ?

Some displays change colour (in part) when they gain focus.
You can get a Graphics object, but I do not know whether that is the same object which the JVM passes for painting.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's given on page- 364 !!
The para just above the code.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And i still don't understand why is there a need for it.. ??? :\
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have had a chance to find my copy of HFJ:

I am not sure I understand what you are querying. It says specifically on page 364 that you
never call this method [paintComponent()] yourself
It also says that you can call repaint(). The fourth paragraph on p364 seems very clear to me. It starts “All of your graphics code”.
I would suggest three enhancements to paintComponent as shown there:-
  • 1: Override annotation, which you doubtless already know about.
  • 2: Access should be protected.
  • 3: super call: particularly important when things move on the display. Removes previous painting.
  • Minor style thing:- More recent versoins of Color have changed, so you would prefer Color.ORANGE, but Color.orange is still available.
     
    Andy Richard
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @CampBell yeah, i got cleared to that!!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!