• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Consecutive Method Calls on a Single Object  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't remember where but I faintly recall one of the oracle docs examples (dealing with GUI basics I think) having something similar to

but only in the aspect of using .method.method.method


The reason I'm asking is because I want to shorten this

Into something like this


I might be way off though... but I'm just wondering under what circumstances would this be possible

Edit: When I try it in Eclipse a little notification appears that says...
"The method add(PopupMenu) in the type Component is not applicable for the arguments (JLabel)"
When I google 'Component' I see it appears to have something to do with Serialization (which I'll need to look into)

This isn't some crucial piece, but it would be nice to know what's going on. Thanks for reading and sorry if this is a bit unorganized or just an insanely childish question
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can if you wish; you would have to change the add method to read like this:-You can find \u00b2 means ² here.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you want to google more information on it the technique is called 'method chaining' or a 'fluent interface'.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect, just what I wanted. thank you both
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look up the Container#add() methods, you find their return type is Container. They have obviously been designed with the idea of method chaining in mind.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!