Recipe 15.12 Program: BusCard


This program may seem a bit silly, but it's a good example of configuring a variety of user interface controls from a resource bundle. The BusCard program allows you to create a digital business card ("interactive business card") onscreen (see Figure 15-2). The labels for all the GUI controls, and even the pull-down menu options, are loaded from a ResourceBundle.

Figure 15-2. BusCard program in action
figs/jcb2_1502.gif


Example 15-2 shows the code for the BusCard program.

Example 15-2. BusCard.java
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; /** Display your business-card information in a Java window.  *  * This is a first attempt. The next version should use a GridBagLayout.  */ public class BusCard extends JFrame {     JLabel nameTF;     JComboBox jobChoice;     JButton B1, B2, B3, B4;     /** "main program" method - construct and show */     public static void main(String[] av) {         // create a BusCard object, tell it to show up         new BusCard( ).setVisible(true);     }     /** Construct the object including its GUI */     public BusCard( ) {         super( );         Container cp = getContentPane( );         cp.setLayout(new GridLayout(0, 1));         addWindowListener(new WindowAdapter( ) {             public void windowClosing(WindowEvent e) {                 setVisible(false);                 dispose( );                 System.exit(0);             }         });         JMenuBar mb = new JMenuBar( );         setJMenuBar(mb);         ResourceBundle b = ResourceBundle.getBundle("BusCard");         JMenu aMenu;         aMenu = I18N.mkMenu(b, "filemenu");         mb.add(aMenu);         JMenuItem mi = I18N.mkMenuItem(b, "filemenu", "exit");         aMenu.add(mi);         mi.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent e) {                 System.exit(0);             }         });         aMenu = I18N.mkMenu(b, "editmenu");         mb.add(aMenu);         aMenu = I18N.mkMenu(b, "viewmenu");         mb.add(aMenu);         aMenu = I18N.mkMenu(b, "optionsmenu");         mb.add(aMenu);         aMenu = I18N.mkMenu(b, "helpmenu");         mb.add(aMenu);         //mb.setHelpMenu(aMenu);        // needed for portability (Motif, etc.).         setTitle(I18N.getString(b, "card"+".company", "TITLE"));         JPanel p1 = new JPanel( );         p1.setLayout(new GridLayout(0, 1, 50, 10));         nameTF = new JLabel("My Name", JLabel.CENTER);         nameTF.setFont(new Font("helvetica", Font.BOLD, 18));         nameTF.setText(I18N.getString(b, "card"+".myname", "MYNAME"));         p1.add(nameTF);         jobChoice = new JComboBox( );         jobChoice.setFont(new Font("helvetica", Font.BOLD, 14));         // Get Job Titles ofrom the Properties file loaded into "b"!         String next;         int i=1;         do {             next = I18N.getString(b, "job_title" + i++, null);             if (next != null)                 jobChoice.addItem(next);         } while (next != null);         p1.add(jobChoice);         cp.add(p1);         JPanel p2 = new JPanel( );         p2.setLayout(new GridLayout(2, 2, 10, 10));         B1 = new JButton( );         B1.setLabel(I18N.getString(b, "button1.label", "BUTTON LABEL"));         p2.add(B1);         B2 = new JButton( );         B2.setLabel(I18N.getString(b, "button2.label", "BUTTON LABEL"));         p2.add(B2);         B3 = new JButton( );         B3.setLabel(I18N.getString(b, "button3.label", "BUTTON LABEL"));         p2.add(B3);         B4 = new JButton( );         B4.setLabel(I18N.getString(b, "button4.label", "BUTTON LABEL"));         p2.add(B4);         cp.add(p2);         pack( );     } }

See Also

Other things may need to be internationalized as well:


Character comparisons

These are set separately on Unix/POSIX; on other operating systems, they depend on the default Locale.


Date and time formats

See GregorianCalendar and DateFormat in the Introduction to Chapter 6.


Number formats

See java.util.NumberFormat in Recipe 5.8.


Message insertions

These appear in different orders in different languages (something the C-language printf( ) could never handle). See java.util.MessageFormat in Recipe 14.10.

Sun maintains an Internationalization Home Page at http://java.sun.com/j2se/corejava/intl/.

15.12.2 Internationalization Caveats

Internationalizing your menus and push buttons is only one step. You also need to internationalize message text in dialogs as well as help files (see the JavaHelp API at http://java.sun.com/products/javahelp/).

Some items such as AWT FileDialog use native components; their appearance depends on the native operating system (your application can change its own default locale, but not the system's. If your customer has a differently internationalized copy of the same OS, the file dialogs will appear differently).

15.12.3 Documentation

A short, readable, non-Java-specific introduction to the overall topic of internationalization is The Guide to Translation and Localization, written by the staff of Lingo Systems and published by the IEEE Computer Society. For more on Java I18N, see the online documentation that ships with the JDK; start at docs/guide/intl/index.html. See also Java Internationalization (O'Reilly).

15.12.4 The Last Word

Good luck. Bonne chance. Buena suerte . . . .



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net