• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Design Pattern. Same settings instances.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have (in Swing) JMenuBar and I want add many JMenu(s). This menus have same settings like width, margin, etc.

And repeat the code like:
JMenu menu1 = new JMenu("First");
menu1.setHorizontalTextPosition(SwingConstants.CENTER);
menu1.setVerticalTextPosition(SwingConstants.BOTTOM);
menu1.setMargin(new Insets(10,0,10,0));
menu1.setIcon(new ImageIcon(EditorWindow.class.getResource("/1.png")));

JMenu menu2 = new JMenu("Second");
menu2.setHorizontalTextPosition(SwingConstants.CENTER);
menu2.setVerticalTextPosition(SwingConstants.BOTTOM);
menu2.setMargin(new Insets(10,0,10,0));
menu2.setIcon(new ImageIcon(EditorWindow.class.getResource("/2.png")));

is not nice.

Is there some design pattern for this situation? I thinking about Prototype, but JMenu hasn't clone method by default.
 
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Petra, welcome to the Ranch!

"Factory method" pattern:


 
Petra Cervinkova
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for welcome and help :-).
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome. But it isn't only a design pattern, it's also a good programming habit: when you see duplicated code (or nearly duplicated code), make a method out of that code.
 
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Different version:It is usually awkward, though, to start handling methods with more than three parameters.

And welcome again
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Petra Cervinkova wrote:Is there some design pattern for this situation? I thinking about Prototype, but JMenu hasn't clone method by default.


My choice would be Builder ... but I'm no GUI expert.

Winston
 
You showed up just in time for the waffles! 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
reply
    Bookmark Topic Watch Topic
  • New Topic