• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java [Chapter 12: Twobuttons]  RSS feed

 
Bhanu Prakash Paruchuri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading this book for quite some time. The explanation given for each and every topic in this book is very clear. However, in this chapter I came across a concept that I am struggling to understand and move forward. I don't think Head First author's would have missed explaining it. I am just wondering if the explanation is covered in any of the pages in the book. Below mentioned are the page and code details.

HeadFirst Java - 2nd edition.

Page 379:

In the TwoButtons program, I could not understand the below statement. It would be great if anywhere in the book there is an explanation for the statement like this.
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);

I am clear until frame.getContentPane(), but after that there is another method add(BorderLayout.SOUTH, colorButton) that is added to the statement. I am looking for this explanation.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have removed the Google Books link because it shows so much of the text that is can be a breach of copyright: and it doesn't even include page 379. But fortunately your question is reasonably clear and I don't think you need the book page to explain it. Interestingly enough, somebody else asked a similar question recently: here. You are correct. If you read about the JFrame#getContentPane() method, you find it returns a Container reference and the Container has an add(Component) method. Now, you will say, those aren't the right types; they are AWT classes not Swing. But if you look at a couple of Swing classes in the documentation you will find they are subclasses of Component and JFrame is a subtype of Container, so they inherit those methods and the types passed are acceptable as arguments to those methods.
The add method is overloaded, as you will find if you go through the documentation links. It can accept a constraint object or a String, which includes BorderLayout.SOUTH. It shou‍ld probably read ...add(myComponent, BorderLayout.SOUTH); because that version of add is described as obsolete.
 
Bhanu Prakash Paruchuri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,
Thanks for the explanation. That makes me understand how method chaining works. I would like to have more practice on this concept. Do you have any reference?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't think of any references just at the moment, I am afraid.
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhanu Prakash Paruchuri wrote:That makes me understand how method chaining works. I would like to have more practice on this concept.

Presumably someone has described it somewhere, but it's pretty basic.

If a method returns an Object, then you can do things with that Object, including calling one of its methods.

For example: Color.GREEN.darker().toString().length()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!