Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(JPanel) getContentPane() vs getContentPane()

Status
Not open for further replies.

theundergod

IS-IT--Management
Feb 4, 2005
30
0
0
US
I am just curious. I am learning Java on my own and the latest book I bought tends to use a typecast of JPanel for the contentPane and I am wondering: since typcasting with JPanel gives you more options like setting the border for the whole JFrame, among others, why would you ever just use getContentPane() by itself with out the typecast? Can anyone explain the pros and cons of using it without the typecast and with it? Thanks for any info.

Jon
 
getContentPane() returns a Container, that is a superclass for JPanel. If you want to access methods inside Container class you don't need the cast, but if you want to access methods from JPanel class (suppossing it's one) you need the cast.

Cheers,
Dian
 
i guess what i am really asking is if you use a typcast can't you access the methods of the ancestor class? if i am not wrong then what would be the point of not using a typecast? maybe to just simplify the code if it is unnecessary to access the JPanel methods. thanks for the reply. it is very much appreciated.

Jon
 
As Dian said - if you wish to access the parent class's methods, then don't bother casting ; but if you wish to access specific methods inherent to the class you expect, then cast.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
theundergod, you do typecast to a JPanel you can still use the methods of the 'ancestor' class (Container). Applying the cast gives you access to all the public methods within JPanel too, but is obviously not necessary unless you intend to use them.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks for the replies Diancecht, sedj, and Timw. Just wanted to make sure I understood entirely.

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top