An interesting Swing feature is added to J2SE version 1.4. Swing controls can now provide auditory feedback to respond to specific events, but this feature is disabled by default. This new feature helps Swing controls better mimic the behavior of controls on the system's local operating system.
Basic knowledge of hearing
Swing uses an pluggable look-and-feel (pluggable Look-and-feel,plaf) architecture. Developers do not have to hard-code for settings such as colors and fonts for different components, and instead, the components will request these settings from the user Interface Manager (UI) manager). As a developer, you can tell the UI manager what kind of interface you want the user to see; You can choose Windows, Motif, or Metal styles, and so on. The UI manager can then actually tell how each component should show itself. For a component like a button, the foreground color is controlled by setting the Button.foreground property, as follows:
Uimanager.put ("Button.foreground", color.red);
Here, Button.foreground is the user interface property name, and color.red represents a specific setting. After you change the settings, the foreground color of all new buttons will be red. (There are also ways to change the foreground color of previously created buttons.) Although different preset skins and sensations provide defaults for these settings, you can reset them.
Under J2SE 1.4, you can enable auditory feedback in a similar way. All you need to know is the UI property name and the appropriate settings. property is named here as Auditorycues.playlist, set to a String array of auditory hint names. The UI Manager will then map these names to the sound files that will play when a particular operation occurs.
The following will give a list of the supported sounds in the look and feel provided by the system. Their names are inherently explanatory.
Checkboxmenuitem.commandsound
Internalframe.closesound
Internalframe.maximizesound
Internalframe.minimizesound
Internalframe.restoredownsound
Internalframe.restoreupsound
Menuitem.commandsound
Optionpane.errorsound
Optionpane.informationsound
Optionpane.questionsound
Optionpane.warningsound
Popupmenu.popupsound
Radiobuttonmenuitem.commandsound
An array of String names provided to the Auditorycues.playlist property is the case-a collection of event names. The UI Manager is responsible for mapping these names to look and feel-specific sounds.
You can manually create an array of event names that you really want to support from these names, but this is not required. Thankfully, there are already two system-defined settings available for commonly used groups, and a setting that can be used to Metal look and feel.
These settings provide a lookup key auditorycues.allauditorycues that allows you to find an array of all sounds from the UI manager. Once you have looked up the array, you can use the Auditorycues.playlist key to store it in the UI manager, as follows:
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.allAuditoryCues"));