Recipe 15.5 Creating a Dialog with I18N Resources


Problem

You want to internationalize a dialog.

Solution

Use a ResourceBundle.

Discussion

This is similar to the use of ResourceBundle in the previous recipes and shows the code for an internationalized version of the JOptionDemo program from Recipe 14.7:

package com.darwinsys.util; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /**  * I18N'd JOptionPane  */ public class JOptionDemo extends JFrame {     ResourceBundle rb;     // Constructor     JOptionDemo(String s) {         super(s);         Container cp = getContentPane( );         cp.setLayout(new FlowLayout( ));         rb = ResourceBundle.getBundle("Widgets");         JButton b = I18N.mkButton(rb, "getButton");         b.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent e) {                 JOptionPane.showMessageDialog(                     JOptionDemo.this,                     rb.getString("dialog1.text"),                     rb.getString("dialog1.title"),                     JOptionPane.INFORMATION_MESSAGE);             }         });         cp.add(b);         b = I18N.mkButton(rb, "goodbye");         b.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent e) {                 System.exit(0);             }         });         cp.add(b);         // the main window         setSize(200, 150);         pack( );     }     public static void main(String[] arg) {         JOptionDemo x = new JOptionDemo("Testing 1 2 3...");         x.setVisible(true);     } }



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