Cocoa Tutorial for Java Programmers
Total Page:16
File Type:pdf, Size:1020Kb
Cocoa Tutorial for Java Programmers 2006-10-03 Java and all Java-based trademarks are Apple Computer, Inc. trademarks or registered trademarks of Sun © 2002, 2006 Apple Computer, Inc. Microsystems, Inc. in the U.S. and other All rights reserved. countries. Simultaneously published in the United No part of this publication may be States and Canada. reproduced, stored in a retrieval system, or Even though Apple has reviewed this document, transmitted, in any form or by any means, APPLE MAKES NO WARRANTY OR mechanical, electronic, photocopying, REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS recording, or otherwise, without prior DOCUMENT, ITS QUALITY, ACCURACY, written permission of Apple Computer, Inc., MERCHANTABILITY, OR FITNESS FOR A with the following exceptions: Any person PARTICULAR PURPOSE. AS A RESULT, THIS DOCUMENT IS PROVIDED “AS IS,” AND is hereby authorized to store documentation YOU, THE READER, ARE ASSUMING THE on a single computer for personal use only ENTIRE RISK AS TO ITS QUALITY AND ACCURACY. and to print copies of documentation for IN NO EVENT WILL APPLE BE LIABLE FOR personal use provided that the DIRECT, INDIRECT, SPECIAL, INCIDENTAL, documentation contains Apple’s copyright OR CONSEQUENTIAL DAMAGES notice. RESULTING FROM ANY DEFECT OR INACCURACY IN THIS DOCUMENT, even if The Apple logo is a trademark of Apple advised of the possibility of such damages. Computer, Inc. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE AND IN Use of the “keyboard” Apple logo LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer, agent, (Option-Shift-K) for commercial purposes or employee is authorized to make any without the prior written consent of Apple modification, extension, or addition to this may constitute trademark infringement and warranty. unfair competition in violation of federal Some states do not allow the exclusion or limitation of implied warranties or liability for and state laws. incidental or consequential damages, so the above limitation or exclusion may not apply to No licenses, express or implied, are granted you. This warranty gives you specific legal with respect to any of the technology rights, and you may also have other rights which vary from state to state. described in this document. Apple retains all intellectual property rights associated with the technology described in this document. This document is intended to assist application developers to develop applications only for Apple-labeled or Apple-licensed computers. Every effort has been made to ensure that the information in this document is accurate. Apple is not responsible for typographical errors. Apple Computer, Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, Aqua, Carbon, Cocoa, iTunes, Mac, Mac OS, Quartz, and Xcode are trademarks of Apple Computer, Inc., registered in the United States and other countries. Finder is a trademark of Apple Computer, Inc. Objective-C is a registered trademark of NeXT Software, Inc. Contents Introduction Introduction to Cocoa Tutorial for Java Programmers 7 Organization of This Document 8 See Also 9 Chapter 1 Creating the Currency Converter Project and User Interface 11 Creating the Currency Converter Project 11 Open Xcode 11 Choose the New Project Command 12 Choose a Project Type 12 Creating the Currency Converter User Interface 15 What Is a Nib File? 15 Open the Main Nib File 16 Windows in Cocoa 16 Resize the Window 17 Set the Window’s Title and Other Attributes 19 Set the Application Name in the Menu 19 Configure a Text Field 19 Duplicate an Object 21 Change the Attributes of a Text Field 21 Assign Labels to the Fields 21 Configure a Button 23 Add a Horizontal Decorative Line 24 Interface Layout and Object Alignment 25 Finalize the Window Layout 25 Enable Tabbing Between Text Fields 26 Set the First Responder for the Currency Converter Window 27 Test the Interface 28 Defining the ConverterController Class 29 Classes and Objects 29 Specify the ConverterController Class 29 Paths for Object Communication: Outlets, Targets, and Actions 30 Define the User Interface and Model Outlets of the ConverterController Class 33 Define the Actions of the ConverterController Class 34 Interconnecting the ConverterController Class and the User Interface 35 Create an Instance of the ConverterController Class 35 Connect the ConverterController Class to the Text Fields 35 3 2006-10-03 | © 2002, 2006 Apple Computer, Inc. All Rights Reserved. CONTENTS Connect the Convert Button to the ConverterController convert Action Method 36 Defining the Converter Class 37 Chapter 2 Implementing Currency Converter 39 Generate the Source Files 39 Place the Implementation Files in the Appropriate Group 40 Implement the Currency Converter Classes 41 Chapter 3 Building Currency Converter 43 Overview of the Build Process 43 Build the Currency Converter Application 43 Look Up Documentation 44 Run Currency Converter 44 Correct Build Errors 44 Great Job! 45 Chapter 4 Expanding on the Basics 47 Application and Window Behavior 47 Controls and Text 47 Menu Commands 48 Document Management 48 File Management 48 Communicating With Other Applications 49 Custom Drawing and Animation 49 Internationalization 49 Editing Support 49 Printing 49 Help 50 Plug-in Architecture 50 Chapter 5 Adopting Objective-C 51 Learn the Objective-C Language 51 Learn the Cocoa Class Hierarchy 52 Learn Memory Management in Cocoa 52 Document Revision History 55 4 2006-10-03 | © 2002, 2006 Apple Computer, Inc. All Rights Reserved. Figures and Listings Introduction Introduction to Cocoa Tutorial for Java Programmers 7 Figure I-1 The Currency Converter main window 7 Chapter 1 Creating the Currency Converter Project and User Interface 11 Figure 1-1 The Xcode application icon 11 Figure 1-2 Xcode New Project Assistant 12 Figure 1-3 Entering a project’s name and choosing its location in Xcode New Project Assistant 13 Figure 1-4 The new Currency Converter project in Xcode 14 Figure 1-5 Resizing a window manually 18 Figure 1-6 Resizing a window with the NSWindow inspector 18 Figure 1-7 Cocoa text controls in the Interface Builder palette window 20 Figure 1-8 Resizing a text field 20 Figure 1-9 Right-aligning a text label in Interface Builder 22 Figure 1-10 Aligned text fields and labels in Interface Builder 23 Figure 1-11 Measuring distances in Interface Builder 24 Figure 1-12 Adding a horizontal line to the Currency Converter window 24 Figure 1-13 Currency Converter’s final user interface in Interface Builder 26 Figure 1-14 Connecting nextKeyView outlets in Interface Builder 27 Figure 1-15 Setting the initialFirstResponder outlet in Interface Builder 28 Figure 1-16 Subclassing java.lang.Object 30 Figure 1-17 An outlet pointing from one object to another 30 Figure 1-18 Relationships in the target-action paradigm 32 Figure 1-19 Outlets and actions in the ConverterController inspector 34 Figure 1-20 A newly instantiated ConverterController instance 35 Figure 1-21 Connecting ConverterController to the rateField outlet 36 Figure 1-22 Connecting ConverterController.converter to the Converter instance 38 Chapter 2 Implementing Currency Converter 39 Listing 2-1 Implementation of the Converter class 41 Listing 2-2 Implementation of the ConverterController class 42 Chapter 3 Building Currency Converter 43 Figure 3-1 Identifying build errors 45 5 2006-10-03 | © 2002, 2006 Apple Computer, Inc. All Rights Reserved. FIGURES AND LISTINGS 6 2006-10-03 | © 2002, 2006 Apple Computer, Inc. All Rights Reserved. INTRODUCTION Introduction to Cocoa Tutorial for Java Programmers Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial Using Objective-C. This document introduces the Cocoa application environment using the Java language and teaches you how to leverage Apple’s development tools to build robust, object-oriented applications. Cocoa provides the best way to build modern, multimedia-rich, object-oriented applications for consumers and enterprise customers alike. This document assumes you are familiar with Java programming but does not assume you have previous experience with Cocoa or Xcode Tools. This document is intended for Java programmers interested in developing Cocoa applications. Keep in mind, however, that Java is not Cocoa’s native language. To develop Cocoa applications that you intend to release to end users, you must use Objective-C. No Java interfaces for new Cocoa features will be added to Mac OS X versions after 10.4. Therefore, features added to Cocoa in subsequent versions Mac OS X will not be available to Cocoa applications developed using Java. This document shows how to build Currency Converter, an application that converts a dollar amount to an amount in another currency, given the rate of that currency relative to the dollar. The main window of the finished application is shown in Figure I-1. Figure I-1 The Currency Converter main window 7 2006-10-03 | © 2002, 2006 Apple Computer, Inc. All Rights Reserved. INTRODUCTION Introduction to Cocoa Tutorial for Java Programmers Currency Converter is a simple application, yet it exemplifies much of what software development with Cocoa is all about. As you’ll discover, Currency Converter is amazingly easy to create, and it’s equally amazing how many features you get “for free”—as with all Cocoa applications. This document leads you through the basic steps for creating a Cocoa application. It shows how to: ■ Create an Xcode project ■ Create a graphical user interface using Interface Builder ■ Create a custom subclass of a Cocoa framework class ■ Connect an instance of your custom subclass to the user interface ■ Build an application and correct problems By following the instructions provided in this document, you familiarize yourself with the two most important applications used for developing Mac OS X applications: Interface Builder and Xcode.