ebook img

Mastering JavaFX 8 Controls PDF

338 Pages·2014·14.6 MB·English
by  Ebbers
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mastering JavaFX 8 Controls

Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3/ Blind folio: i ® Mastering JavaFX® 8 Controls Hendrik Ebbers New York Chicago San Francisco Athens London Madrid Mexico City Milan New Delhi Singapore Sydney Toronto 00-FM.indd 1 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Copyright © 2014 by McGraw-Hill Education (Publisher). All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of Publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. ISBN: 978-0-07-183378-3 MHID: 0-07-183378-1 e-Book conversion by Cenveo® Publisher Services Version 1.0 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-183377-6, MHID: 0-07-183377-3. McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative, please visit the Contact Us pages at www.mhprofessional.com. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these marks. Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corporation and/or its affiliates. Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use of such information. Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any information contained in this Work, and is not responsible for any errors or omissions. TERMS OF USE This is a copyrighted work and McGraw-Hill Education (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. eBook 377-3 CR_pg.indd 1 6/5/14 5:31 PM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 For Silke—Because you helped me every day writing this book. I wrote this book in my free time, so there were weeks where I completely disappeared from normal home life. You managed everything alone, and in addition you backed me when I became frustrated about this project. Without you, I couldn’t have managed this. For the JavaFX community—Because without the awesome community, I would have never been in the position to write this book. For my father—Because you taught me that you can create everything you imagine if you have enough courage to take hold of it. For all of my friends and family—Because you heard me talking about this book too many times but still listened to me. 00-FM.indd 3 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 About the Author Hendrik Ebbers is senior Java architect at Materna GmbH in Dortmund, Germany. His main focus besides research and development is primarily in the areas of JavaFX, middleware, and DevOps. Additionally, Hendrik is founder and leader of the Java User Group Dortmund and gives talks and presentations in user groups and international conferences. He blogs about UI-related topics at www.guigarage .com (or on Twitter @hendrikEbbers) and contributes to some open source projects such as DataFX, BoxFX, AquaFX, and Vagrant-Binding. About the Technical Editor Simon Ritter works as a Java technology evangelist for Oracle Corporation and Sun Microsystems before that. He has been developing Java code since JDK 1.0 and has been involved in JavaFX since its launch as a scripting language. 00-FM.indd 4 5/21/14 11:17 AM Contents Introduction ......................................................... ix 1 The History of Java UI Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Java SE UI Toolkits .................................................... 2 AWT ......................................................... 2 Java Foundation Classes and the Emergence of Swing .................... 3 Swing ........................................................ 3 Additional UI Toolkits .................................................. 5 SWT ......................................................... 5 Apache Flex ................................................... 5 The Way to JavaFX .................................................... 5 From F3 to JavaFX 8 ............................................. 6 JavaFX Compared to HTML5 and Web-Based Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Java-Based Web Frameworks ...................................... 8 Summary ........................................................... 8 2 JavaFX Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Your First JavaFX Application ............................................ 10 JavaFX Application Life Cycle ...................................... 12 Defining the Main Window by Using the Stage Class .................... 12 The Scene Graph ............................................... 18 Technical Design of the JavaFX Toolkit ..................................... 19 The Native Layer ................................................ 19 Private API Layer ................................................ 20 Public API Layer ................................................ 20 JavaFX Public APIs .................................................... 20 Application and Life Cycle ........................................ 20 Stage API ..................................................... 20 Scene Graph and Controls ........................................ 20 v 00-FM.indd 5 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 vi Mastering JavaFX 8 Controls Event Handling ................................................. 21 Property API ................................................... 22 Collections .................................................... 23 Concurrent API ................................................. 24 Animations .................................................... 24 FXML ........................................................ 25 CSS Support ................................................... 25 Printing ....................................................... 26 Interoperability with Swing ........................................ 26 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Scene Builder .................................................. 27 Scenic View ................................................... 27 FX Experience Tools ............................................. 28 Deployment/Native Builds .............................................. 29 JavaFX Goes Polyglott .................................................. 30 GroovyFX ..................................................... 30 ScalaFX ...................................................... 31 JavaFX and Nashorn ............................................. 32 Summary ........................................................... 33 3 The Scene Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Using and Integrating the Scene Graph in a JavaFX Application .................. 36 The Scene Class ...................................................... 37 Event Handling ....................................................... 44 Node Types ......................................................... 46 Primitive Nodes ................................................ 46 LayoutPanes ................................................... 48 Complex Nodes ................................................ 49 Node Basics ......................................................... 50 FXML .............................................................. 56 Summary ........................................................... 60 4 Laying Out and Transforming Nodes in the Scene Graph . . . . . . . . . . . . . . . . 61 Adding Some Transformations ............................................ 62 Adding a Third Dimension ........................................ 66 Extended Transformation APIs ...................................... 66 Laying Out Nodes .................................................... 70 Creating a Custom Pane .......................................... 71 The Visual Structure of a Region .................................... 74 Extended Internal Layout Mechanisms ............................... 81 Additional Layout Mechanisms ..................................... 85 The javafx.geometry Package ...................................... 85 Working with Constraints ......................................... 85 Combining Transforms and Layout ........................................ 86 Accessing the Bounds of a Node ................................... 88 Summary ........................................................... 88 00-FM.indd 6 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 vi Mastering JavaFX 8 Controls Contents vii 5 JavaFX Basic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Control Class ..................................................... 90 Basic Controls ....................................................... 91 Labeled Controls ............................................... 95 Controls for Text Input ........................................... 101 Slider ........................................................ 106 ProgressIndicator and ProgressBar .................................. 110 Tooltip of a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Using Menus in JavaFX ................................................. 114 Using Separators ................................................ 117 Creating an Application with Basic Controls ................................. 119 Summary ........................................................... 126 6 Additional JavaFX Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Controls with a Data Model ............................................. 128 ComboBox .................................................... 128 ListView ...................................................... 134 TableView .................................................... 150 TreeView ..................................................... 165 TreeTableView ................................................. 171 Sorting and Filtering Data ......................................... 173 Controls That Act as Containers .......................................... 176 Additional Controls ................................................... 181 HTMLEditor ......................................................... 181 DatePicker .................................................... 183 ColorPicker ................................................... 185 An Interview with Jonathan Giles, Engineer on the JavaFX Team, Oracle . . . . . . . . . . . 186 Summary ........................................................... 190 7 Additional JavaFX Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Charts .............................................................. 192 WebView ........................................................... 195 Canvas ....................................................... 202 ImageView .......................................................... 206 MediaView .......................................................... 210 Summary ........................................................... 211 8 Integrating JavaFX, Swing, and SWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Combining JavaFX and Swing ............................................ 214 Using the JFXPanel .................................................... 214 Using the SwingNode ............................................ 219 Using the Experimental Single-Thread Mode .......................... 221 Pros and Cons of the Integration .................................... 222 Combining JavaFX and SWT ............................................. 223 Using the FXCanvas ............................................. 223 Summary ........................................................... 226 00-FM.indd 7 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 viii Mastering JavaFX 8 Controls 9 Styling a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Using Themes to Style an Application ...................................... 228 CSS Basics .......................................................... 230 CSS in JavaFX ........................................................ 232 Using Selectors ................................................. 237 Summary of the Cascading Feature .................................. 247 Styling a Chart ................................................. 248 Best Practices for Styling Applications and Controls ..................... 250 An Interview with Claudine Zillmann, software developer at maredit GmbH . . . . . . . 254 Summary ........................................................... 257 10 Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 The Structure of a Control ............................................... 260 The Skin ...................................................... 261 The SkinBase Class .............................................. 262 Creating a Custom Control .............................................. 264 Adding Event Handling ........................................... 270 Styling the Control .............................................. 272 An Interview with Gerrit Grunwald, Canoo Engineering . . . . . . . . . . . . . . . . . . . . . . . 300 Summary ........................................................... 303 A JavaFX Resources and Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . 305 Make Your UI Shine ................................................... 306 JavaFX-Related Middleware and Application Frameworks ....................... 306 DataFX ....................................................... 307 OpenDolphin .................................................. 308 Best of Open Source Projects ............................................ 308 Important JavaFX Links ................................................. 309 JavaFX Books ........................................................ 309 JavaFX Application .................................................... 310 Summary ........................................................... 310 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 00-FM.indd 8 5/21/14 11:17 AM Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Oracle TIGHT / Mastering JavaFX 8 Controls / Hendrik Ebbers / 377-3 Introduction JavaFX from a Developer’s Point of View My background is in a lot of Swing development and web application creation. For web applications, I mostly used plain HTML or JSF to create the views, and I did some little applications with most of the other technologies (Adobe Flex, Flash, Android, Wicket, GWT, and so on) to learn more about these UI toolkits over the years. For me, JavaFX is a perfect combination of most of the best practices that have come out of these technologies. For a Swing developer, most of the basic JavaFX APIs are easy to learn because the main concepts seem to be similar when looking at the framework. Under the hood, most of the technologies are different, but you can structure an application in a better way by using FXML and CSS, for example. Compared to HTML applications, JavaFX development is much easier for me because theoretically, you can do whatever you want and don’t need to think about cross-browser behavior and all that stuff. You can create the complete view by using What You See Is What You Get (WYSIWYG) editors like Scene Builder and use your favorite Java IDE for development. JavaFX offers a small learning curve, so you’ll be able to create your first applications quickly. In addition, because of the good documentation and structure of the APIs, you probably won’t make as many mistakes as you might have when learning Swing or HTML, for example. But, there are some places where you need to know the underlying technology well, and some parts are hard when you experiment the first time with them. When creating my first custom JavaFX control, I needed a lot of help to understand all the concepts and APIs involved. That was before JavaFX 8, though, and most of the documentation that is available today didn’t exist. For me, as a Java developer, JavaFX is the best choice to develop applications that don’t need to be captured in a browser. With the release of Java 8, JavaFX is the default UI toolkit for Java. In this book, you will learn how to master JavaFX, especially the control API, to create applications based on these technologies. You can get all the scripts and programs featured in this book online (see the section “Retrieving the Examples” for details). ix 00-FM.indd 9 5/21/14 11:17 AM

Description:
Find out how to work with the latest JavaFX APIs, configure UI components, automatically generate FXML, build cutting-edge controls, and effectively apply CSS styling. Troubleshooting, tuning, and deployment are also covered in this guide. You will learn the properties of JavaFX 8 controls and APIs;
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.