Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin.

Similar presentations


Presentation on theme: " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin."— Presentation transcript:

1  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin College

2  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/16) Review: Listeners To handle widget events, we implement Listener interfaces: public class ListenersExample1 extends JFrame { private JComboBox myBox; // generates ActionEvents private JSlider mySlider; // generates ChangeEvents... }... private class BoxListener implements ActionListener { public BoxListener() {} public void actionPerformed(ActionEvent ae) {... } } private class SliderListener implements ChangeListener { public SliderListener() {} public void stateChanged(ChangeEvent ce) {... } } myBox.addActionListener( new BoxListener() ); mySlider.addChangeListener( new SliderListener() );

3  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/16) Review: Listeners  Events  Widgets Swing provides many widgets, that trigger various events ListenerEventWidgets ActionListener ActionEventJButton JComboBox JTextField JCheckBox AdjustmentListener AdjustmentEvent JScrollbar ChangeListener ChangeEventJSlider KeyListener KeyEventkeyboard MouseListener MouseEventmouse MouseMotionListener MouseEventmouse JRadioButton JMenu

4  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/16) A Nuisance Some Listeners have several methods that must be defined: public class ListenersExample2 extends JFrame {... } private class ClickListener implements MouseListener { public ClickListener() {} public void mouseClicked(MouseEvent me) {... } public void mousePressed(MouseEvent me) {... } public void mouseReleased(MouseEvent me) {... } public void mouseEntered(MouseEvent me) {... } public void mouseExited(MouseEvent me) {... } } A class that implements the interface must supply a definition for each method, even if it only needs one of them...

5  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/16) Example Here is code that grabs the coordinates of a mouse-click: public class ListenersExample2 extends JFrame { private JPanel myMainPanel = new JPanel();... myMainPanel.addMouseListener( new ClickListener() ); } private class ClickListener implements MouseListener { } public void mouseClicked(MouseEvent me) { myMousePosition = me.getPoint(); //... Do whatever is needed at myMousePosition } public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mousePressed(MouseEvent me) {} public void mouseReleased(MouseEvent me) {} Isn’t there a better way?

6  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/16) Solution: Adaptors An adaptor is a standard Java class that implements an interface, supplying an empty definition for each method… A Listener class that is not extending any other class can extend the adaptor instead of implementing the interface. class MouseAdaptor implements MouseListener { public MouseAdaptor() {} public void mouseClicked(MouseEvent me) {} public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mousePressed(MouseEvent me) {} public void mouseReleased(MouseEvent me) {} }

7  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/16) Adaptor Example public class ListenersExample3 extends JFrame { private JPanel myMainPanel = new JPanel();... myMainPanel.addMouseListener( new ClickListener() );... } private class ClickListener extends MouseAdaptor { } public void mouseClicked(MouseEvent me) { myMousePosition = me.getPoint(); //... Do whatever is needed at myMousePosition } Since our ClickListener class extends the adaptor, overides its mouseClicked() method, and inherits the other methods, we need not define those other methods. Package java.awt.event provides Java’s adaptors.

8  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/16) Exercise: Part I Recall: Java char variables are 16-bit Unicode variablesUnicode Unicode has “space” (2 16 == 65,536 values) for the characters of most of the world’s languages. Take a few minutes to experiment with Part I of today’s exercise, looking over the source code and trying a variety of 16-bit base-values (0..65,535). Use unicode.org to verify Java’s support for the following character sets:  Cyrillic  Arabic  Tamil  Thai

9  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/16) Internationalization Java supports internationalization several ways:  Unicode  Timezones  Locales  Dates, Currency, Numbers, … Timezones are straightforward, so we’ll focus on locales… A locale contains:  a language  a location  a variant (optionally) In the U.S., we usually use the locale (English, US) In Germany, we’d probably use (German, Germany) In Switzerland, we might use (German, Switzerland) or (French, Switzerland) or (Italian, Switzerland) or …

10  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/16) Using Locales: Number Formatting Different countries “punctuate” numbers differently: import java.text.NumberFormat; import java.util.Locale; public class DecimalFormat { public static void main(String args[]) { NumberFormat nf1 = NumberFormat.getInstance(); // default System.out.println(nf1.format(1234.56)); NumberFormat nf2 = NumberFormat.getInstance(Locale.GERMAN); System.out.println(nf2.format(1234.56)); } Output: 1,234.56 1.234,56 We use the NumberFormat class to get an instance of a locale’s number format, and then send it the format() message to format a number for that locale.

11  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/16) Using Locales: Currency Formatting import java.util.*; public class CurrencyDemo { public static void main(String args[]) { Currency c1 = Currency.getInstance(Locale.US); System.out.println("US Dollar symbol = " + c1.getSymbol()); System.out.println("US Dollar symbol in Canada: " + c1.getSymbol(Locale.CANADA)); String jc = Currency.getInstance("JPY").getCurrencyCode()); System.out.println("Currency code for Japan = " + jc); String gc = Currency.getInstance(Locale.GERMANY).getCurrencyCode(); System.out.println("Currency code for Germany = " + gc); } Output: US Dollar symbol = $ US Dollar symbol in Canada = USD Currency code for Japan = JPY Currency code for Germany = EUR

12  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(12/16) Using Locales: Date Formatting import java.text.*; import java.util.*; public class DisplayDates { public static void main(String args[]) { Locale locale = null; if (args.length == 1) { locale = new Locale(args[0]); } else if (args.length == 2) { locale = new Locale(args[0], args[1]); } else { locale = Locale.getDefault(); } Date now = new Date(); // or Date now = new GregorianCalendar().getTime();...

13  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(13/16) Using Locales: Date Formatting (ii)... DateFormat format = DateFormat.getDateInstance( DateFormat.SHORT, locale); System.out.println("Short: " + format.format(now)); format = DateFormat.getDateInstance( DateFormat.MEDIUM, locale); System.out.println("Medium: " + format.format(now)); format = DateFormat.getDateInstance( DateFormat.LONG, locale); System.out.println("Long: " + format.format(now)); format = DateFormat.getDateInstance( DateFormat.FULL, locale); System.out.println("Full: " + format.format(now)); format = DateFormat.getDateInstance( DateFormat.DEFAULT, locale); System.out.println("Default: " + format.format(now)); }

14  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(14/16) Using Locales: Date Formatting (iii) $ java DisplayDates us Short: 11/4/03 Medium: Nov 4, 2003 Long: November 4, 2003 Full: Tuesday, November 4, 2003 Default: Nov 4, 2003 Sample Executions: $ java DisplayDates de Short: 04.11.03 Medium: 04.11.2003 Long: 4. November 2003 Full: Dienstag, 4. November 2003 Default: 04.11.2003 $ java DisplayDates fr Short: 04/11/03 Medium: 4 nov. 2003 Long: 4 novembre 2003 Full: mardi 4 novembre 2003 Default: 4 nov. 2003 $ java DisplayDates es Short: 4/11/03 Medium: 04-nov-2003 Long: 4 de noviembre de 2003 Full: martes 4 de noviembre de 2003 Default: 04-nov-2003

15  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(15/16) Summary Java provides significant support for internationalization, by which a program’s look-and-feel can be customized to one with which local users are familiar. Besides its use of Unicode, Java’s Locale class is a key component in making this happen, along with:  NumberFormat, to format numbers correctly; XML-based resource files can also be used to localize an application’s labels, button-labels, menu-item-labels, …  Currency, to format monetary values correctly;  DateFormat, to format dates correctly; and  Date, Calendar, and TimeZone to format times correctly.

16  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(16/16) Exercise: Parts II and III Use the remaining time to work through parts II and III of today’s exercise. We will be demo-ing your Part III solutions at the end of today’s session!


Download ppt " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin."

Similar presentations


Ads by Google