Useful Java Snippets (4)

evertwagenaar.com logo

Hello all of you Java geeks!

Welcome back to a new episode of this historical java snippet series! In this episode we will continue having fun with the java.util package.

Beyond Collections

This far we sticked with Collections and Arrays but there’s more. The Date and Calendars for example are all part of the same family, just like the Tokenizer, Event notifiers, Randomizer, Localizers and more. Enough to write about! .

Locales

Locales identify a specific language and geographic region. Locale-sensitive objects use java.util.Locale objects to customize how they present and format data to the user. Locales affect user interface language, case mapping, collation (sorting), date and time formats, and number and currency formats. It’s a good practice to use Locales in your Java Apps since they make it possible to publish your Apps for more countries and languages.

1. Listing all available locales

Locale[] locales = Locale.getAvailableLocales();

for (int i=0; i<locales.length; i++) {
// Get the 2-letter language code
String language = locales[i].getLanguage();

// Get the 2-letter country code; may be equal to ""
String country = locales[i].getCountry();

// Get localized name suitable for display to the user
String locName = locales[i].getDisplayName();
}
  1. A sample of output using a default locale of Locale.ENGLISH:
Language Code, Country Code, Localized Name
ar, AE, Arabic (Arab Emirates)
ar, BH, Arabic (Bahrain)
ar, DZ, Arabic (Algeria)
ar, EG, Arabic (Egypt)
ar, IQ, Arabic (Iraq)
ar, JO, Arabic (Jordan)
ar, KW, Arabic (Kuwait)
ar, LB, Arabic (Lebanon)

  1. Locale.FRENCH:
Language Code, Country Code, Localized Name
ar, , arabe
ar, AE, arabe (Emirats Arabes Unis)
ar, EG, arabe (Egypte)
ar, IQ, arabe (Irak)
ar, JO, arabe (Jordanie)
ar, KW, arabe (Koweit)
ar, LB, arabe (Liban)

Events

An event in Java is an object that is created when something changes within a graphical user interface. … If a user clicks on the JButton, a button click event is triggered, the event will be created, A new custom event must extends EventObject. Moreover, an event listener interface must be declared to allow objects to receive the new custom event. All listeners must extend from EventListener.
This example demonstrates all the steps necessary to create a new custom event.

  1. Declare an event:
// Declare the event. It must extend EventObject.
public class MyEvent extends EventObject {
  public MyEvent(Object source) {
  super(source);
  }
 }

// Declare the listener class. It must extend
   EventListener.
// A class must implement this interface to get
   MyEvents.
   public interface MyEventListener extends
   EventListener {
   public void myEventOccurred(MyEvent evt);
  }

  // Add the event registration and notification
     code to a class.
   public class MyClass {
   // Create the listener list
  protected javax.swing.event.EventListenerList listenerList =
new javax.swing.event.EventListenerList();

// This methods allows classes to register for MyEvents
public void addMyEventListener(MyEventListener listener) {
listenerList.add(MyEventListener.class, listener);
}

// This methods allows classes to unregister for MyEvents
public void removeMyEventListener(MyEventListener listener) {
listenerList.remove(MyEventListener.class, listener);
}

// This private class is used to fire MyEvents
    void fireMyEvent(MyEvent evt) {
     Object[] listeners =
     listenerList.getListenerList();
    // Each listener occupies two elements - the 
       first is the listener class
   // and the second is the listener instance
   for (int i=0; i<listeners.length; i+=2) {
    if (listeners[i]==MyEventListener.class) {
      ((MyEventListener)listeners[i+1])
        .myEventOccurred(evt);
    }
  }
 }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.