• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Calling Same Method for Multiple Objects

 
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).
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java 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.
 
Marshal
Posts: 25682
69
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.
 
Marshal
Posts: 67420
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Marshal
Posts: 25682
69
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:


 
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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.
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic