Menu created from property file : I18N « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » I18NScreenshots 
Menu created from property file

//Menus.properties

/*
# The file Menus.properties is the default "Menus" resource bundle.
# As an American programmer, I made my own locale the default.
colors.label=Colors
colors.red.label=Red
colors.red.accelerator=alt R
colors.green.label=Green
colors.green.accelerator=alt G
colors.blue.label=Blue
colors.blue.accelerator=alt B

*/

//Menus_fr.properties
/*
# This is the file Menus_fr.properties.  It is the resource bundle for all
# French-speaking locales.  It overrides most, but not all, of the resources
# in the default bundle.
colors.label=Couleurs
colors.red.label=Rouge
colors.green.label=Vert
colors.green.accelerator=control shift V
colors.blue.label=Bleu

*/

//Menus_en_GB.properties
/*
# This is the file Menus_en_GB.properties.  It is the resource bundle for
# British English.  Note that it overrides only a single resource definition
# and simply inherits the rest from the default (American) bundle.
colors.label=Colours


*/

///
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/** 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");
      catch (MissingResourceException e) {
        itemlabel = itemnames[i];
      }

      JMenuItem item = new JMenuItem(itemlabel);

      // Look up an accelerator for the menu item
      try {
        String acceleratorText = bundle.getString(menuname + "."
            + itemnames[i".accelerator");
        item.setAccelerator(KeyStroke.getKeyStroke(acceleratorText));
      catch (MissingResourceException e) {
      }

      // Register an action listener and command for the item.
      if (listener != null) {
        item.addActionListener(listener);
        item.setActionCommand(itemnames[i]);
      }

      // Add the item to the menu.
      menu.add(item);
    }

    // Return the automatically created localized menu.
    return menu;
  }

  /** A simple test program for the above code */
  public static void main(String[] args) {
    // Get the locale: default, or specified on command-line
    Locale locale;
    if (args.length == 2)
      locale = new Locale(args[0], args[1]);
    else
      locale = Locale.getDefault();

    // Get the resource bundle for that Locale. This will throw an
    // (unchecked) MissingResourceException if no bundle is found.
    ResourceBundle bundle = ResourceBundle.getBundle(
        "com.davidflanagan.examples.i18n.Menus", locale);

    // Create a simple GUI window to display the menu with
    final JFrame f = new JFrame("SimpleMenu: " // Window title
        locale.getDisplayName(Locale.getDefault()));
    JMenuBar menubar = new JMenuBar()// Create a menubar.
    f.setJMenuBar(menubar)// Add menubar to window

    // Define an action listener for that our menu will use.
    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        Component c = f.getContentPane();
        if (s.equals("red"))
          c.setBackground(Color.red);
        else if (s.equals("green"))
          c.setBackground(Color.green);
        else if (s.equals("blue"))
          c.setBackground(Color.blue);
      }
    };

    // Now create a menu using our convenience routine with the resource
    // bundle and action listener we've created
    JMenu menu = SimpleMenu.create(bundle, "colors"new String[] { "red",
        "green""blue" }, listener);

    // Finally add the menu to the GUI, and pop it up
    menubar.add(menu)// Add the menu to the menubar
    f.setSize(300150)// Set the window size.
    f.setVisible(true)// Pop the window up.
  }
}



           
       
Related examples in the same category
1. I18N SortI18N Sort
2. Java I18N: Format : Choice Format DemoJava I18N: Format : Choice Format Demo
3. Java I18N: Format : Date FormatJava I18N: Format : Date Format
4. Java I18N: Format : Date Format Symbols DemoJava I18N: Format : Date Format Symbols Demo
5. Java I18N: Format : Message Format DemoJava I18N: Format : Message Format Demo
6. Java I18N: Format : Number FormatJava I18N: Format : Number Format
7. Java I18N: Format : Simple Date FormatJava I18N: Format : Simple Date Format
8. Java I18N: IntroductionJava I18N: Introduction
9. List all LocaleList all Locale
10. Displays Charsets and aliasesDisplays Charsets and aliases
11. Encode and DecodeEncode and Decode
12. List Locale OrientationList Locale Orientation
13. Resource Bundle in Java code
14. List Resource Bundle Creator
15. Simple Resource Bundle
16. Your own Resource Bundle
17. Displaying Formatted Numbers for Alternate LocalesDisplaying Formatted Numbers for Alternate Locales
18. Convert Encoding
19. Unicode DisplayUnicode Display
20. Demonstrate number and date internationalizationDemonstrate number and date internationalization
21. Set of convenience routines for internationalized code
22. Print the default locale
23. Use some locales choicesUse some locales choices
24. Format some values using the default locale
25. List LocalesList Locales
26. Create one button, internationalizedly
27. Show DateShow Date
28. Change the default locale
29. java.util.Locale and java.text.NumberFormatjava.util.Locale and java.text.NumberFormat
30. Collation Test Collation Test
31. Number Format Test Number Format Test
32. Java Internationalization: load string from properties Java Internationalization: load string from properties
33.  Locales  Locales
34. Locale 2Locale 2
35. Display Language OutputDisplay Language Output
36. Display Name OutputDisplay Name Output
37. Display Variant OutputDisplay Variant Output
38. Constant Locale UsageConstant Locale Usage
39. Country Language CodesCountry Language Codes
40. Isolating Locale-Specific Data with Resource Bundles Isolating Locale-Specific Data with Resource Bundles
41. Property To List Resource Bundle
42. Which Bundle Comes First
43. Calendar Manipulation for I18N (Internationalization)Calendar Manipulation for I18N (Internationalization)
44. Formatting Messages: Arabic DigitFormatting Messages: Arabic Digit
45. Formatting Messages: Change EraFormatting Messages: Change Era
46. Formatting Messages: Message Format ReuseFormatting Messages: Message Format Reuse
47. Character Sets and Unicode: Code Set Conversion
48. Searching, Sorting, and Text Boundary Detection: Collation IssuesSearching, Sorting, and Text Boundary Detection: Collation Issues
49. Searching, Sorting, and Text Boundary Detection: Detecting Text BoundariesSearching, Sorting, and Text Boundary Detection: Detecting Text Boundaries
50. Spanish SortSpanish Sort
51. Sort With Collation KeysSort With Collation Keys
52. DecompositionDecomposition
53. Formatting Messages: Date and NumberFormatting Messages: Date and Number
54. Internationalized Graphical User Interfaces: Component OrientationInternationalized Graphical User Interfaces: Component Orientation
55. Internationalized Graphical User Interfaces: Component Orientation 1Internationalized Graphical User Interfaces: Component Orientation 1
56. Localized JOptionPane
57. Big Demo for I18N
58. Component Orientation Bundle
59. Java Input Method Framework
60. Popup in FrenchPopup in French
61. I18N : TextI18N : Text
w__w_w._j___a__v_a___2s_._c___om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.