Books for professionals By professionals ® Beginning JavaFX ™ Dear Reader, Beginning JavaFX ™ introduces you to Rich Internet Applications (RIA) and explains to you all the essential features of the JavaFX ™ Platform in depth. JavaFX ™ is a new technology and its documentation is still evolving, so we offer you here a deeper insight into the JavaFX ™ platform, while keeping things as clear as possible so that you can begin to start writing practical code with JavaFX ™ whether you have worked with graphics before or not. We've designed this book to be as hands-on as possible for you by including many code samples and a code-driven methodology that will explain all the JavaFX ™ features that you need in detail. Beginning JavaFX ™ covers a wide range of topics, from the basics of RIA, to the diverse language and UI constructs that it makes available to you. This book guides you through basic language features such as data types, expres- sions, functions, operators, and class definitions, as well as advanced JavaFX ™ features such as binding, triggers, access specifiers, and inheritance. We also give you a very detailed explanation of many user interface aspects that you'll be glad you considered in your projects - such as graphics, controls, and ani- mation. You'll immediately find yourself writing practical and thoughtful code with JavaFX ™ with us! Lawrence PremKumar Praveen Mohan US $39.99 Shelve in: Programming Languages / Java User level: Beginner – Intermediate Praveen Mohan THE APRESS ROADMAP Pro JavaFX™ Platform Beginning JavaFX™ Beginning Java™ SE 6 Platform www.apress.com SOURCE CODE ONLINE Companion eBook Lawrence PremKumar PremKumar Mohan Companion eBook Available JavaFX ™ Beginning Lawrence PremKumar and Praveen Mohan Beginning JavaFX ™ The eXperT’s Voice ® in JaVa ™ Technology A hands-on tutorial for learning and using JavaFX ™ to build your next Java ™ rich client or Rich Internet Application (RIA) www.it-ebooks.info www.it-ebooks.info Beginning JavaFX™ ■ ■ ■ LAWRENCE PREMKUMAR PRAVEEN MOHAN www.it-ebooks.info ii Beginning JavaFX™ Copyright © 2010 by Lawrence PremKumar and Praveen Mohan All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-7199-4 ISBN-13 (electronic): 978-1-4302-7198-7 Printed and bound in the United States of America (POD) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Tom Welsh Technical Reviewer: Sten Anderson Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Kelly Moritz Copy Editor: James A. Compton Compositor: Kimberly Burton Indexer: Toma Mulligan Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info I dedicate this book to my beloved parents, Andrew and Jyothimani, brothers Bharath and Ranjith, wife Lavanya Lawrence, daughters Angel , Merlin and Bincy Lawrence and finally my friends Vimala Anne, Karkinath and Ravindra — Lawrence PremKumar I dedicate this book to my beloved parents, Dr. Prem Mohan and Sakunthala —Praveen Mohan www.it-ebooks.info i v Contents at a Glance ■About the Authors xiv ■About the Technical Reviewer xv ■Acknowledgements xvi ■Introduction xvii ■Chapter 1: Introduction to RIA 1 ■Chapter 2: Introduction to JavaFX 9 ■Chapter 3: Data Types 33 ■Chapter 4: Operators and Expressions 47 ■Chapter 5: Functions 75 ■Chapter 6: Class Definitions 91 ■Chapter 7: Access Specifiers 109 ■Chapter 8: Inheritance 141 ■Chapter 9: Data Binding 155 ■Chapter 10: Sequences 175 ■Chapter 11: Triggers 189 ■Chapter 12: Introduction to JavaFX UI Elements 203 ■Chapter 13: Introduction to Animation 269 ■Index 303 www.it-ebooks.info v Contents ■About the Authors xiv ■About the Technical Reviewer xv ■Acknowledgements xvi ■Introduction xvii ■Chapter 1: Introduction to RIA 1 The History of RIA 1 Key Characteristics of RIA 2 RIA Workflow 2 Why RIA 3 Some RIA Examples 5 Summary 7 ■Chapter 2: Introduction to JavaFX 9 Why JavaFX 9 Advantages of JavaFX 9 History of JavaFX 10 The JavaFX Platform 11 The Developer Bundle 11 The Designer Bundle 12 Standalone 12 JavaFX Platform Integration: The Bigger Picture 13 JavaFX Mobile: An Introduction 14 Advantages of JavaFX Mobile 14 www.it-ebooks.info ■ CONTENTS vi vi Deployment and Distribution 15 Getting Started 16 What to Download 16 Writing Your First JavaFX Application 16 Running Your Application Using NetBeans 21 Running the Application from the Command Line 27 Comments 30 Summary 31 ■Chapter 3: Data Types 33 Variable Declaration 33 var vs. def Declarations 34 Variable Naming 34 Variable Declaration Syntax 35 Data Types 36 Integer 39 Number 40 Boolean 41 Duration 42 Typecasting 43 Sequences 44 Default Values for Data Types 45 Summary 46 ■Chapter 4: Operators and Expressions 47 The Assignment Operator 48 The as Operator 49 Arithmetic Operators 50 The Modulus or Remainder Operator 51 www.it-ebooks.info ■ CONTENTS vii vii The Arithmetic Assignment Operator 51 Operator Precedence 52 Unary Operators 52 The Increment and Decrement Operators: ++ and 53 The Unary + and – Operators 54 The not Operator 55 Relational Operators 55 Logical Operators 56 Range Expressions 59 Block Expressions 61 Looping Expressions 62 While Loops 66 Break Expressions 66 Continue Expressions 67 The if-else Expression 68 Exception Handling 70 The new Expression 71 Differentiating Expressions 72 Summary 73 ■Chapter 5: Functions 75 How a Function Works 77 A Function with Neither an Argument nor a Return Value 77 A Function with Arguments but Without a Return Value 78 A Function Without an Argument but with a Return Value 79 A Function with Arguments and a Return Value 80 Variable Access within a Function 81 Script-Level Variables 81 Local Variables 82 www.it-ebooks.info ■ CONTENTS viii viii Function Overloading 82 Recursive Functions 83 Anonymous Functions 84 The run() Function 86 Command-Line Arguments 87 Summary 89 ■Chapter 6: Class Definitions 91 Classes and Objects 91 Classes 91 Objects 92 Features of OOP 92 Data Abstraction 92 Encapsulation 92 Inheritance 92 Polymorphism 92 The Class Definition 93 Creating Object Literals 94 Initializing Class Attributes within an Object Literal 95 Calling the Members of the Class 95 Assigning Default Values to Data Members 97 The init Block 97 The postinit Block 99 Modifying Class Objects 99 Objects as Function Arguments 100 Non-Member Functions Accessing the Object 101 Static Members 102 Sharing a Function Name Between Script-Level and Member Functions 104 www.it-ebooks.info [...]... stand-alone JavaFX 1.2.1 SDK The SDK includes the following components (also included when you download NetBeans IDE for JavaFX) : JavaFX Desktop Runtime JavaFX Mobile Emulator (for Windows) JavaFX APIs JavaFX Compiler JavaFX API documentation Examples 12 www.it-ebooks.info CHAPTER 2 ■ INTRODUCTION TO JAVAFX JavaFX Platform Integration: The Bigger Picture Figure 2-2 illustrates the bigger picture of how the JavaFX. .. it released JavaFX 1.2 at the JavaOne 2009 conference At press time, Sun is currently working on JavaFX 1.3, tentatively targeted to be released in early 2010 10 www.it-ebooks.info CHAPTER 2 ■ INTRODUCTION TO JAVAFX The JavaFX Platform The JavaFX 1.2.1 platform release includes various components, as illustrated in Figure 2-1 Let us see each one of them in detail Figure 2-1 The JavaFX platform: an... bigger picture of how the JavaFX platform integrates different platform elements, the runtime, tools, and frameworks to deliver applications, content, and services to consumers using multiple devices Figure 2-2 The JavaFX Platform: the bigger picture Here is a brief introduction to each of the elements illustrated in Figure 2.2: JavaFX Runtime: Contains cross -platform and platform- specific runtime environments... is bringing expressiveness to the most powerful and pervasive mobile platform On mobile devices, JavaFX runs directly on Java ME to take advantage of the platform s ubiquity, security, and highly capable feature-set With JavaFX Mobile, developers and designers benefit from using the same JavaFX tools, such as the JavaFX SDK and the JavaFX Production Suite, that they have been using to create content... Windows platform (Future releases of JavaFX may also have an emulator available on the Mac platform. ) If you already have NetBeans IDE: Update your IDE with JavaFX 1.2.1 Plug-in for NetBeans This version is available for Windows and Mac OS X platforms A beta release is provided for the Ubuntu Linux and OpenSolaris platforms The plug-ins provide the features that support the development of JavaFX applications... assets The JavaFX platform contains an essential set of tools and technologies that enable developers and designers to collaborate, create, and deploy applications with expressive content Advantages of JavaFX JavaFX is a full-fledged development platform for RIAs and has many advantages over other equivalent technologies in the market Out of all, there are some key factors that differentiate JavaFX significantly... from the combination of Java and JavaFX Mobile Advantages of JavaFX Mobile Here are the advantages of using the JavaFX Mobile platform to develop and deliver expressive content: • • JavaFX makes it easy to design dynamic interfaces that integrate audio, video, text, graphics, and animation! • Java is backed by nearly all operators and OEMs, making it the strongest platform in the industry • 14 You... front of more users than with any other platform by cutting across multiple OEMs and platforms JavaFX Mobile lets operators and OEMs build on their existing investment in Java to lower their implementation costs www.it-ebooks.info CHAPTER 2 ■ INTRODUCTION TO JAVAFX • JavaFX Mobile allows developers to build expressive interfaces while reusing existing Java code • JavaFX Mobile provides the broadest access... for JavaFX 1.2.1 This version is available for Windows and Mac OS X platforms A beta release is available for Ubuntu and OpenSolaris platforms The NetBeans IDE for JavaFX 1.2.1 is a full-featured development environment that is packaged with the JavaFX Software Development Kit (SDK) and with bestpractice examples that can help you build your software development project The installation includes the JavaFX. .. already using the NetBeans IDE, you can add the JavaFX plug-in to include support for developing JavaFX applications JavaFX Plug-in for Eclipse IDE: Sun also offers a JavaFX plug-in for the Eclipse IDE, which works with Eclipse IDE 3.4 or newer 11 www.it-ebooks.info CHAPTER 2 ■ INTRODUCTION TO JAVAFX The Designer Bundle The designer bundle consists of JavaFX Production Suite, a single download that . Introduction to JavaFX 9 Why JavaFX 9 Advantages of JavaFX 9 History of JavaFX 10 The JavaFX Platform 11 The Developer Bundle 11 The Designer Bundle 12 Standalone 12 JavaFX Platform Integration:. professionals ® Beginning JavaFX ™ Dear Reader, Beginning JavaFX ™ introduces you to Rich Internet Applications (RIA) and explains to you all the essential features of the JavaFX ™ Platform in depth. JavaFX ™ . Platform Beginning JavaFX Beginning Java™ SE 6 Platform www.apress.com SOURCE CODE ONLINE Companion eBook Lawrence PremKumar PremKumar Mohan Companion eBook Available JavaFX ™ Beginning Lawrence