Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

labels in java gui components

 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all.
Is there good coding practice for putting labels in java gui controls? If I'm coding a java gui application, is it ok to hard code the labels...like for example...
Button b = new Button("Ok");
thanks.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Dennis,
Thats what i do,
here is a piec of code i did for my distance learning course:

Davy
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry bearing in mind that the above posted code is a constructor, with these member variables:

Davy
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with that, especially if you know you will not have to internationalize the application. If so, you may want to put the message Strings in a resource bundle and pull them out as needed in the app. It's also worth noting that some IDE's, like NetBeans, provide some support for helping to do this internationalization after the fact.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Davy,Ken...looks good. Thank you for your replies.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having written 2 applications that are available to the world, I now put all Strings into a ResourceBundle from the start of a project. This helps in two ways:
1) If you ever i18n, the work is already done for you.
2) As I write the app, I tend to change the strings displayed to the user fairly often til I get it right. By having all the strings in one place, I don't have to hunt down which class the String is defined in and I can change a string without recompiling.
Just my $0.02
Brian
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Swing/AWT.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian. Thanks for your reply. Seems like a good idea but is there a performance-related issue with your approach?
thanks.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time a label on a component is set one time (at creation time) and a lookup of the string for it in a ResourceBundle will take negligible time compared to setting up the rest of the Swing/SWT/GUI. So, no there should not be a noticeable performance hit. Even if you change the label after it's created, a simple lookup of the string does not make any noticeable difference in the GUI. Download and try either of my apps (links in sig) - both are i18n'd - every piece of text you see is stored in a ResourceBundle (a .properties file) which is stored in the program's .jar file.
Brian
[ February 10, 2004: Message edited by: Brian Pipa ]
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome. Thanks Brian.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic