Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2008

Tip: Looking for answers? Try searching our database.

To use paint or paintComponent?

Thread view: 
travel2light - 15 Mar 2008 21:44 GMT
Which is the right one to use for Swing components? I have always used
JPanels with a method paint(Graphics g) and then just called that
method using 'repaint()'. But after reading some of the discussions
here I found out that the correct way to go is to use
paintComponent(Graphics g). Why is this? What is the difference
between the two methods? Thanks for any advice.

Michael
Jeff Higgins - 16 Mar 2008 01:18 GMT
> Which is the right one to use for Swing components? I have always used
> JPanels with a method paint(Graphics g) and then just called that
[quoted text clipped - 4 lines]
>
> Michael

<http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks>
<http://java.sun.com/products/jfc/tsc/articles/painting/index.html>
Roedy Green - 16 Mar 2008 06:59 GMT
On Sat, 15 Mar 2008 13:44:34 -0700 (PDT), travel2light
<everything2light@yahoo.co.uk> wrote, quoted or indirectly quoted
someone who said :

>Which is the right one to use for Swing components? I have always used
>JPanels with a method paint(Graphics g) and then just called that
>method using 'repaint()'. But after reading some of the discussions
>here I found out that the correct way to go is to use
>paintComponent(Graphics g). Why is this? What is the difference
>between the two methods? Thanks for any advice.

See http://mindprod.com/jgloss/paintcomponent.html
http://mindprod.com/jgloss/paint.html

Yes, you should use paintComponent.  calling super.paintComponent and
using setOpaque replaces the old update/paint mechanism.

You can trace the calls or look at the code in source.zip, but when I
did that, I could not understand what the new scheme bought them.
I just use it.  
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Daniel Pitts - 27 Mar 2008 18:13 GMT
> On Sat, 15 Mar 2008 13:44:34 -0700 (PDT), travel2light
> <everything2light@yahoo.co.uk> wrote, quoted or indirectly quoted
[quoted text clipped - 16 lines]
> did that, I could not understand what the new scheme bought them.
> I just use it.  

paint in JComponent is designed to call  paintComponent and paintBorder,
and handle delegation to the PLAF, as well as handle double buffering.

If you override "paint" you basically create a "heavy-weight" AWT
component rather than a "light-weight" Swing component.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.