File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What are multiple dots in a line really saying (class.method.method) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What are multiple dots in a line really saying (class.method.method)" Watch "What are multiple dots in a line really saying (class.method.method)" New topic
Author

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

David Ausman
Greenhorn

Joined: Jan 04, 2009
Posts: 12
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
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


Kannan.DV
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

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


Dhruva
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jan 04, 2009
Posts: 12
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What are multiple dots in a line really saying (class.method.method)
 
Similar Threads
JFrames and JPanel
Certification Advice
Tag Lib calling a class
Multiple lock requests on the same record number
why java not providing multiple inheritance