Describing GUIs with Properties

At its core, the task of specifying a graphical user interface is a descriptive one. This descriptive task does not map well onto a procedural and algorithm-based programming language such as Java. You end up writing lots of code that creates components, sets properties, and adds components to containers. Instead of simply describing the structure of the GUI you want, you must write the step-by-step code to build the GUI.

One way to avoid writing this tedious GUI construction code is to create a GUI-description language of some sort, then write code that can read that language and automatically create the described GUI. One common approach is to describe a GUI using an XML grammar. In this chapter, we’ll rely on the simpler syntax of Java properties files as used by the ResourceBundle class. (See Chapter 8 for examples using java.util.ResourceBundle.)

A java.util.Properties object is a hashtable that maps string keys to string values. The Properties class can read and write a simple text file format in which each name:value line defines a single property. Furthermore, a Properties object can have a parent Properties object. When you look up the value of a property that does not exist in the child Properties object, the parent Properties object is searched (and this continues recursively). The ResourceBundle class provides an internationalization layer around properties files that allows properties to be customized for use in different locales. ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.