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

Slides:



Advertisements
Similar presentations
TCU CoSc Programming with Java Handling Events.
Advertisements

Event Handling.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Processing Date and Time Date class Date currentTime = new Date(); DateFormat class The DateFormat class can be used to format date and time in a number.
Intermediate Java1 An example that uses inner classes Week Four Continued.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
For (int i = 1; i
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling and Listeners in SWING The practice of event handling.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
Events and Event Handling
GUI III IS
Event Handling CS 21a: Introduction to Computing I
GUI Programming III: Events
Event-driven programming for GUI
Chapter 12 Event Handling
Inner Classes 29-Nov-18.
Events, Event Handlers, and Threads
Final project.
Inner Classes.
Presentation transcript:

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

 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() );

 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

 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...

 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?

 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) {} }

 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.

 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

 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 …

 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( )); NumberFormat nf2 = NumberFormat.getInstance(Locale.GERMAN); System.out.println(nf2.format( )); } Output: 1, ,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.

 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

 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();...

 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)); }

 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: Medium: Long: 4. November 2003 Full: Dienstag, 4. November 2003 Default: $ java DisplayDates fr Short: 04/11/03 Medium: 4 nov Long: 4 novembre 2003 Full: mardi 4 novembre 2003 Default: 4 nov $ 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

 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.

 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!