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.
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.
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Joined: Mar 07, 2003
Hi Davy,Ken...looks good. Thank you for your replies.
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
Hi Brian. Thanks for your reply. Seems like a good idea but is there a performance-related issue with your approach? thanks.
Joined: Sep 29, 2003
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 ]