• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Component init best practices

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if the title is mis-leading, but I had a discussion with a buddy of mine the other day about properly setting up a given component. I have always started panels or components with all the super calls first ( like setsize,setvisible and all things related in that manner) and than after I would add all my necessary buttons,panels,etc,etc.. And he said he had always done it the opposite way. I do understand there are some things like a layout manager that must be called first obviously, though not always, before adding more components. I was simply wondering what the proper or maybe best way to do these things?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setSize doesn't work with layoutmanagers
setVisible is not required for non-toplevel(?) containers - frames, dialogs, internalFrames etc
"super calls first" indicates inheritance. google 'composition Vs inheritance' and read "Effective Java"

as for best practices - generally the best way is the way the boss does it, or can follow it
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No no, I should not have pointed out setvisible or setsize, maybe that was not a great example. This is how i typically code a JComponent..


How I will typically set up a panel or component, is with this call to init and than add all components that need to be added. My buddy was simply saying that all components should be added first than set the size and all that jazz. I really don't think it matters either way, but I was just wondering what everyone else does, if that makes things clearer. And as far as your recommend of Effective java, I did read up on it on amazon and it looks like a great book with the second edition not released too long ago, I will def be buying that book sooner than later.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all very subjective of course, but if SomeJComponent extends (say) JPanel,
I'd be inclined to do setLayout, setBackground etc in the constructor.
(I really see no point to init() except in an applet context).
createComponents, called from the constructor, seems fine, but you would
also (generally) have your listeners there too.

personally I like to put the gui (the view) together in one method - components,
listeners etc, leaving the bare minimum (if anything) exposed.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic