JLocaleChooser is a bean for choosing locales. : ComboBox « Swing Components « Java






JLocaleChooser is a bean for choosing locales.

     
/*
 *  JLocaleChooser.java  - A bean for choosing locales
 *  Copyright (C) 2004 Kai Toedter
 *  kai@toedter.com
 *  www.toedter.com
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

//package com.toedter.components;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * JLocaleChooser is a bean for choosing locales.
 * 
 * @author Kai Toedter
 * @version $LastChangedRevision: 85 $
 * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $
 */
public class JLocaleChooser extends JComboBox implements ItemListener {
  private static final long serialVersionUID = 8152430789764877431L;
  protected JComponent component;

  /**
   * Default JLocaleChooser constructor.
   */
  public JLocaleChooser() {
      this(null);
  }

    /**
     * Returns "JLocaleChoose".
     *
     * @return the name value
     */
    public String getName() {
        return "JLocaleChoose";
    }

  /**
   * Default JLocaleChooser constructor.
   */
  public JLocaleChooser(JComponent component) {
    super();
    this.component = component;
    addItemListener(this);
    locales = Calendar.getAvailableLocales();
    localeCount = locales.length;

    for (int i = 0; i < localeCount; i++) {
      if (locales[i].getCountry().length() > 0) {
        addItem(locales[i].getDisplayName());
      }
    }

    setLocale(Locale.getDefault());
  }

  /**
   * The ItemListener for the locales.
   */
  public void itemStateChanged(ItemEvent iEvt) {
    String item = (String) iEvt.getItem();
    int i;

    for (i = 0; i < localeCount; i++) {
      if (locales[i].getDisplayName().equals(item))
        break;
    }
    setLocale(locales[i], false);
  }

  /**
   * Sets the locale.
   * 
   * @see #getLocale
   */
  private void setLocale(Locale l, boolean select) {
    Locale oldLocale = locale;
    locale = l;
    int n = 0;

    if (select) {
      for (int i = 0; i < localeCount; i++) {
        if (locales[i].getCountry().length() > 0) {
          if (locales[i].equals(locale))
            setSelectedIndex(n);
          n += 1;
        }
      }
    }

    firePropertyChange("locale", oldLocale, locale);
    if(component != null) {
        component.setLocale(l);
    }
  }

  /**
   * Sets the locale. This is a bound property.
   * 
   * @see #getLocale
   */
  public void setLocale(Locale l) {
    setLocale(l, true);
  }

  /**
   * Returns the locale.
   */
  public Locale getLocale() {
    return locale;
  }

  /**
   * Creates a JFrame with a JLocaleChooser inside and can be used for
   * testing.
   */
  static public void main(String[] s) {
    JFrame frame = new JFrame("LocaleChooser");
    frame.getContentPane().add(new JLocaleChooser());
    frame.pack();
    frame.setVisible(true);
  }

  private Locale[] locales;
  private Locale locale;
  private int localeCount;
}

   
    
    
    
    
  








Related examples in the same category

1.Swing Table in ComboBoxSwing Table in ComboBox
2.ComboBox color chooser (Windows Color Chooser)ComboBox color chooser (Windows Color Chooser)
3.MSN like Swing ComboBoxMSN like Swing ComboBox
4.Swing Auto Complete ComboBoxSwing Auto Complete ComboBox
5.Auto complete ComboBox
6.Stepped ComboBox ExampleStepped ComboBox Example
7.Block ComboBox ExampleBlock ComboBox Example
8.Disabled ComboBox ExampleDisabled ComboBox Example
9.ToolTip ComboBox ExampleToolTip ComboBox Example
10.ComboBox Menu ExampleComboBox Menu Example
11.Small Cell Combobox ExampleSmall Cell Combobox Example
12.JComboBox: adding automatic completion-Catching user inputJComboBox: adding automatic completion-Catching user input
13.JComboBox: adding automatic completion-Adding automatic selectionJComboBox: adding automatic completion-Adding automatic selection
14.JComboBox: adding automatic completion-Adding automatic completionJComboBox: adding automatic completion-Adding automatic completion
15.JComboBox: adding automatic completion-Fixed Auto SelectionJComboBox: adding automatic completion-Fixed Auto Selection
16.JComboBox: adding automatic completion-Case insensitive matchingJComboBox: adding automatic completion-Case insensitive matching
17.JComboBox: adding automatic completion-Prefer the currently selected itemJComboBox: adding automatic completion-Prefer the currently selected item
18.JComboBox: adding automatic completion-Ignore input that does not matchJComboBox: adding automatic completion-Ignore input that does not match
19.JComboBox: adding automatic completion-Highlight complete textJComboBox: adding automatic completion-Highlight complete text
20.JComboBox: adding automatic completion-Popup the item listJComboBox: adding automatic completion-Popup the item list
21.JComboBox: adding automatic completion-Cursor positionJComboBox: adding automatic completion-Cursor position
22.JComboBox: adding automatic completion-Handling the initial selectionJComboBox: adding automatic completion-Handling the initial selection
23.JComboBox: adding automatic completion-Handling focus lossJComboBox: adding automatic completion-Handling focus loss
24.JComboBox: adding automatic completion-BackspaceJComboBox: adding automatic completion-Backspace
25.JComboBox: adding automatic completion-Backspace 2JComboBox: adding automatic completion-Backspace 2
26.JComboBox: adding automatic completion-Pressing backspace at the beginningJComboBox: adding automatic completion-Pressing backspace at the beginning
27.JComboBox: adding automatic completion-Maximum MatchJComboBox: adding automatic completion-Maximum Match
28.JComboBox: adding automatic completion-Non-strict matchingJComboBox: adding automatic completion-Non-strict matching
29.JComboBox: adding automatic completion-Non-strict matching 2JComboBox: adding automatic completion-Non-strict matching 2
30.JComboBox: adding automatic completion-Binary Lookup and PerformanceJComboBox: adding automatic completion-Binary Lookup and Performance
31.JComboBox: adding automatic completion-Binay Lookup 2JComboBox: adding automatic completion-Binay Lookup 2
32.The KeyedComboBox model allows to define an internal key (the data element) for every entry in the model.
33.Auto Complete ComboBox 2