Java Reference
In-Depth Information
Figure 7-2 shows the menus it creates in the American, British, and French locales.
This program cannot run, of course, without localized resource bundles from
which the localized menu labels are looked up.
Figur e 7−2. Localized menu panes
Example 7−4: SimpleMenu.java
package com.davidflanagan.examples.i18n;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
/** A convenience class to automatically create localized menu panes */
public class SimpleMenu {
/** The convenience method that creates menu panes */
public static JMenu create(ResourceBundle bundle,
String menuname, String[] itemnames,
ActionListener listener)
{
// Get the menu title from the bundle. Use name as default label.
String menulabel;
try { menulabel = bundle.getString(menuname + ".label"); }
catch(MissingResourceException e) { menulabel = menuname; }
// Create the menu pane.
JMenu menu = new JMenu(menulabel);
// For each named item in the menu.
for(int i = 0; i < itemnames.length; i++) {
// Look up the label for the item, using name as default.
String itemlabel;
try {
itemlabel =
bundle.getString(menuname+"."+itemnames[i]+".label");
}
Search WWH ::




Custom Search