Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What are multiple dots in a line really saying (class.method.method)

 
David Ausman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question that's come up while I'm learning Swing and I want to really understand what’s happening with this line of code where frame is of type JFrame and mainPanel is of type JPanel. I can make it work but I don’t really understand why it works. It’s not the BorderLayout that is confusing me but the “.add” method and how it got there:

frame.getContentPane().add(BorderLayout.CENTER.mainPanel);

I’m trying to figure out where that “.add” method comes from and also how to read the Java documentation related to all the parts of this line. I understand that “getContentPanel” is a method from class JFrame so when I look in the Java documentation I see that method under class JFrame. But then, when I go there to read about the method it doesn’t say anything about that “.add” method that follow it. Instead, I see something about “Container”.

If I click on “Container” then I see the “.add” method. But what’s going on here? How is that method attached to the previous method? Is it through inheritance? I’m not getting it.

What does it really mean when I see multiple “.”s in the same line? To me it seems like there can only be one method attached to a class.

Thanks for any help.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it really mean when I see multiple “.”s in the same line? To me it seems like there can only be one method attached to a class.


Basically....

obj.method();

Means to call the method() of the instance a. But what if the method returns an object? and you want to call a method on tthat instance? ... I guess you can do this....

B b = a.method();
b.anotherMethod();

Or to do it directly without the b reference variable....

(a.method()).anotherMethod();

And since the "." operator has left-right assoc, you can get rid of the parens....

a.method().anotherMethod();


Henry
 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see the JFrame ....super classes
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame

which conatins the Container class....
the method getContentPane() of JFrame class returns the 'container' object which has the method 'add()'.
clear....
for BorderLayout.CENTER.mainPanel
BorderLayout.CENTER - is a string object represent a the position in BorderLayout.

i think the mainPanel is a componet...if then
the add method is like....
add (BorderLayout.CENTER , mainPanel)
syntax : public Component add(String name, Component comp )
refer java doc : Container

 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ausman wrote:I have a question that's come up while I'm learning Swing and I want to really understand what’s happening with this line of code where frame is of type JFrame and mainPanel is of type JPanel. I can make it work but I don’t really understand why it works. It’s not the BorderLayout that is confusing me but the “.add” method and how it got there:

frame.getContentPane().add(BorderLayout.CENTER.mainPanel);

I’m trying to figure out where that “.add” method comes from and also how to read the Java documentation related to all the parts of this line. I understand that “getContentPanel” is a method from class JFrame so when I look in the Java documentation I see that method under class JFrame. But then, when I go there to read about the method it doesn’t say anything about that “.add” method that follow it. Instead, I see something about “Container”.

If I click on “Container” then I see the “.add” method. But what’s going on here? How is that method attached to the previous method? Is it through inheritance? I’m not getting it.

What does it really mean when I see multiple “.”s in the same line? To me it seems like there can only be one method attached to a class.

Thanks for any help.





here, start from left to right:
frame is an object used to call method of same class named getContentPane()
and whatever will be the return value, will be act as an object for the method named add(BorderLayout.CENTER.mainPanel)

and this method will also return some value that may get stored in a variable or object thru assignment operator at the left side of it

 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually add(mainPanel, BorderLayout.CENTER) with a comma in the middle.

CENTER is a (static) field of the BorderLayout class and add is a method of the Container class and mainPanel is obviously what you are adding. More details about that add method here.
 
David Ausman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. That is really interesting. It did not dawn on me that the method itself is returning an object so the next dot is the method for the returned object. Cool. Okay, now I get it. That really helps.

Oh, sorry for my typo in the command...yes, I was supposed to put a comma in that last parameter. I hope I didn't confuse everybody.

Now, if I can just master digging through the Java API documentation my life will be complete...

Dave
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ausman wrote:Now, if I can just master digging through the Java API documentation my life will be complete...

Dave
If only life were that simple . . .


And you're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic