Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Management of Java string resources

Tags:

java

I'm working on a java SE 1.5+ swing application, in conjunction with others. I'm wondering what the best way of managing string resources is. I understand the principles behind resource bundles etc. I'd like to avoid having one property file for every class that needs a string, as this seems a bit of overkill. Especially when you have a lot of classes that may only make a single reference to a string (say in an error handler). On the other hand it makes it easier when collaborating with others as you reduce the risk of merge conflicts.

It seems particularly cumbersome to have to load resource bundles, every time you need to display simple user feedback, likewise in error handlers, when many classes are involved.

What is the most effective way to manage strings in a fairly large application with hundreds of classes, many of which aren't GUI related, but need to pass informative messages back up to the GUI when exceptions occur.

I'm using NetBeans which generally creates a property file for each GUI class, for all text relating to buttons, labels etc.


2 Answers

What makes you think you have to have a separate properties file for every class? Generally you only need a few (or just one!) properties file (per language, of course).

Just call ResourceBundle.getBundle() with appropriate parameters - you can use the same bundle from multiple classes.

EDIT: Having one set of property files per dialog etc makes it easier to see where any particular string is coming from, but it makes it harder to reuse the same messages etc. Quite where the right balance is will depend on the application.

like image 84
Jon Skeet Avatar answered Feb 05 '26 11:02

Jon Skeet


JSR 296 Swing Application Framework has support for resource management (and it looks like will be part of Java 7). SAF aims to pre-build parts of a Swing app that many people frequently need while encapsulating best practices. You probably don't want to tie to it directly but its worth taking a look at what they do to see whether it gives you some ideas. If I recall, they use cascading resource bundles with well-defined naming conventions. The latter means you know where to look and the former means that you can reuse properties across some portion of your package hierarchy.

Many JSR 296 resources collected here.

like image 33
Alex Miller Avatar answered Feb 05 '26 11:02

Alex Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!