This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling Same Method for Multiple Objects  RSS feed

 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

Is there a way to call the same method for multiple objects without re-writing the same code as in the example below?


I'm using GridPane and want the same width for all my buttons (preferrably as low as possible).
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make button an array and loop through the array.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if it buys you much, but you could try building a list and looping through it.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what Carey and Knute said.
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I thought there was some way to make it cleaner code-wise. I'll consider making a list (maybe temporary) as I have a few similar operations to make on these same buttons.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could consider a factory pattern:





This has an advantage over an array or list of buttons, namely that you can use variables with meaningful names.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer Paul's approach. Making an array or list out of it is fairly needless.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This little utility method is handy for making components the same width where the new width is the maximum width of the component arguments. Using varargs you can pass in either a comma separated list of components or an array of components.

 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You could consider a factory pattern:



This has an advantage over an array or list of buttons, namely that you can use variables with meaningful names.


Thanks. I haven't thought of it. Would it be even more efficient to pass the new button as an argument (as below), the method serving the purpose of only configuring it? I guess it can't be called a factory method in such case, however...



Thanks for the suggestion Carey. I might not use this exact method, but I'll consider using this approach elsewhere in my small app.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do it that way if you liked. Or you could set the customized properties on the result of the factory method:


 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason to use the Factory approach would be if at some point you wanted to alter the standard button's behaviour, which involved sub-classing JButton. The factory method could hide all that from the application and continue to return an instance of a JButton. No changes would be required for the application code to utilize the new behaviour.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!