Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Initializing GUI Components?  RSS feed

 
Alex Draper
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for ways to initialize a GUI screen and make sure that it is initialized only once. In other words, I am looking for examples that accomplish what the following code does.
if (frame == null) {
frame = new JFrame();
} else {
frame.setTitle("Title");
}
I am looking for a more elegant way to check if a gui component has been initialized. I know a Singleton can be a solution, but I am wondering if someone out there has come up with some way to do this without using the "if !null then create" or singleton approach. Anyone...?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All a Singleton really does in this regard is encapsulate the existence check. "Taking a look" sometimes seems awkward to me too, but there really isn't a programmatic way to conserve the effort of finding out.
The usual alternative is some kind of explicit flag check, such as an isInitialized() method that returns a boolean.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!