ebook img

Java FX for Dummies PDF

436 Pages·2016·4.8 MB·English
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 Java FX for Dummies

JavaFX ® by Doug Lowe JavaF X® For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2015 by John Wiley & Sons, Inc., Hoboken, New Jersey Media and software compilation copyright © 2015 by John Wiley & Sons, Inc. All rights reserved. Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit- ted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permis- sion of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH- OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For tech- nical support, please visit www.wiley.com/techsupport. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2014941051 ISBN 978-1-118-38534-0 (pbk); ISBN 978-1-118-41743-0 (ebk); ISBN 978-1-118-42166-6 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Table of Contents Introduction ................................................................. 1 About This Book ..............................................................................................1 Foolish Assumptions .......................................................................................2 How This Book Is Organized ..........................................................................3 Part I: Getting Started with JavaFX ......................................................3 Part II: JavaFX Controls .........................................................................3 Part III: Enhancing Your Scenic Design ...............................................3 Part IV: Making Your Programs Come Alive .......................................4 Part V: The Part of Tens ........................................................................4 Icons Used in This Book .................................................................................4 Beyond the Book .............................................................................................5 Where to Go from Here ...................................................................................5 Part I: Getting Started with JavaFX ............................... 7 Chapter 1: Hello, JavaFX! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What Is JavaFX? .............................................................................................10 Perusing the Possibilities of JavaFX ............................................................11 Looking at a Simple JavaFX Program ..........................................................13 Downloading and Installing JavaFX .............................................................16 Downloading JDK 8 ..............................................................................16 Installing JDK 8 .....................................................................................17 Setting the path ....................................................................................17 Developing the Click Me Program with Notepad ......................................19 Developing the Click Me Program with TextPad .......................................22 Using an IDE to Create the Click Me Program ............................................24 Chapter 2: Looking Closer at JavaFX Programming . . . . . . . . . . . . . . . 31 Looking Again at the Click Me Program ......................................................31 Importing JavaFX Packages ..........................................................................34 Extending the Application Class ..................................................................34 Launching the Application ...........................................................................36 Overriding the start Method ........................................................................36 Creating a Button ...........................................................................................38 Handling an Action Event .............................................................................39 Creating a Layout Pane .................................................................................41 Making a Scene ...............................................................................................42 Setting the Stage ............................................................................................42 Examining the Click Counter Program ........................................................43 iv JavaFX For Dummies Chapter 3: Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Examining Events ..........................................................................................50 Handling Events .............................................................................................52 Implementing the EventHandler Interface .................................................53 Handling Events with Inner Classes ............................................................57 Handling Events with Anonymous Inner Classes ......................................59 Using Lambda Expressions to Handle Events ............................................61 Chapter 4: Setting the Stage and Scene Layout . . . . . . . . . . . . . . . . . . . 67 Examining the Stage Class ............................................................................68 Examining the Scene Class ...........................................................................72 Switching Scenes ...........................................................................................73 Creating a Dialog Box ....................................................................................77 Creating a Confirmation Box ........................................................................82 Exit, Stage Right .............................................................................................85 Creating a Close button.......................................................................87 Handling the CloseRequest event ......................................................88 Putting it all together...........................................................................90 Chapter 5: Using Layout Panes to Arrange Your Scenes . . . . . . . . . . . 93 Working with Layout Panes ..........................................................................93 Introducing four JavaFX layout panes...............................................94 Creating layout panes..........................................................................95 Combining layout panes .....................................................................96 Using the HBox Layout .................................................................................96 Spacing Things Out .......................................................................................98 Adding Space with Margins ........................................................................100 Adding Space by Growing Nodes ..............................................................101 Using the VBox Layout ................................................................................104 Aligning Nodes in a Layout Pane ...............................................................106 Making Nodes the Same Width ..................................................................107 Using the Flow Layout .................................................................................108 Using the Border Layout .............................................................................113 Chapter 6: Getting Input from the User . . . . . . . . . . . . . . . . . . . . . . . . . 117 Using Text Fields .........................................................................................118 Validating Numeric Data .............................................................................125 Using Check Boxes ......................................................................................126 Using Radio Buttons ....................................................................................129 Looking at a Pizza Order Application .......................................................131 v Table of Contents Part II: JavaFX Controls ............................................ 141 Chapter 7: Introducing the JavaFX Node Hierarchy . . . . . . . . . . . . . . 143 An Overview of JavaFX Packages ..............................................................144 The Node Class ............................................................................................146 The Parent Class ..........................................................................................148 The Region Class .........................................................................................150 The Control Class ........................................................................................153 Chapter 8: Choosing from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Using Choice Boxes .....................................................................................155 Creating a choice box ........................................................................157 Setting a default value .......................................................................158 Getting the selected item ..................................................................159 Working with Observable Lists ..................................................................160 Listening for Selection Changes .................................................................163 Using Combo Boxes .....................................................................................164 Creating combo boxes ......................................................................166 Getting the selected item ..................................................................167 Handling combo box events .............................................................168 Using List Views ...........................................................................................169 Creating a list view ............................................................................171 Getting the selected items ................................................................171 Using Tree Views .........................................................................................172 Building a tree ....................................................................................173 Creating a TreeView control.............................................................177 Getting the selected node .................................................................179 Looking at a complete program that uses a tree view ..................180 Chapter 9: Working with Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Creating the Data for a Table .....................................................................186 Creating a Read-Only Table ........................................................................191 Using the TableColumn class ...........................................................192 Using the TableView class ................................................................194 A Program That Creates a Read-Only Table ............................................196 Creating an Editable Table .........................................................................198 Adding table rows ..............................................................................198 Deleting table rows ............................................................................199 Editing table cells ...............................................................................200 A Program That Creates an Editable Table ..............................................202 Chapter 10: Making Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Introducing Classes for Creating Menus ...................................................209 Creating a Basic Menu Bar .........................................................................210 Creating Menus ............................................................................................211 Creating Menu Items ...................................................................................212 vi JavaFX For Dummies Using Separators ..........................................................................................214 Using Action Listeners ................................................................................214 Creating Menus That Change .....................................................................216 Using Check and Radio Menu Items ..........................................................217 Creating Submenus .....................................................................................220 Creating Custom Menu Items .....................................................................221 Part III: Enhancing Your Scenic Design ...................... 225 Chapter 11: More about Layout Panes for Precise Scene Design . . . 227 Using the StackPane Layout .......................................................................228 Using the TilePane layout ...........................................................................232 Using the ScrollPane Layout ......................................................................236 Using the GridPane Layout .........................................................................241 Sketching out a plan ..........................................................................242 Creating a grid pane ..........................................................................243 Working with grid pane constraints ................................................245 Examining a grid pane example .......................................................248 Chapter 12: Skinning Your Application with CSS . . . . . . . . . . . . . . . . 255 Using Default Style Sheets ..........................................................................256 Adding a Style Sheet to a Scene .................................................................257 Using Inline Styling ......................................................................................260 Creating a Style Sheet .................................................................................260 Using type selectors ..........................................................................261 Creating your own style class names ..............................................262 Using id selectors...............................................................................262 Using multiple selectors ...................................................................263 Specifying Style Properties .........................................................................263 Specifying font properties ................................................................264 Specifying background colors ..........................................................265 Specifying border properties ...........................................................266 Chapter 13: Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Introducing the Shape Class ......................................................................267 Creating lines ......................................................................................271 Creating rectangles ............................................................................272 Creating circles and ellipses.............................................................273 Creating arcs ......................................................................................273 Looking at the ShapeMaker program ..............................................274 Fancy Fills .....................................................................................................277 Drawing transparently ......................................................................277 Using a gradient fill ............................................................................278 Translating, Scaling, and Rotating .............................................................283 Drawing Text ................................................................................................284 Combining Shapes .......................................................................................286 vii Table of Contents Chapter 14: Adding Special Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Introducing Special Effects .........................................................................289 Adding Shadows ..........................................................................................290 Creating Reflections ....................................................................................295 Making Things Blurry ..................................................................................296 Blooming and Glowing ................................................................................298 Gaining Perspective .....................................................................................300 Combining Effects ........................................................................................304 Part IV: Making Your Programs Come Alive ................ 307 Chapter 15: Using Properties to Create Dynamic Scenes . . . . . . . . . 309 Introducing JavaFX Properties ..................................................................309 Java API Properties .....................................................................................311 JavaFX Property Classes .............................................................................312 Creating a Read/Write Property ................................................................314 Creating a Read-Only Property ..................................................................316 Creating Properties More Efficiently .........................................................318 Using Property Events ................................................................................320 Binding Properties .......................................................................................323 Chapter 16: Using Images and Media . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Using Images ................................................................................................327 Using the Image class ........................................................................328 Using the ImageView class................................................................330 Viewing an Image example................................................................332 Playing Audio Files ......................................................................................334 Playing Video Files ......................................................................................337 Chapter 17: Animating Your Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Introducing JavaFX Animation ..................................................................341 Using Transition Classes ............................................................................342 Looking at a Transition Example ...............................................................348 Combining Transitions ...............................................................................351 Animating the Hard Way .............................................................................354 Improving the Ball Bouncer .......................................................................358 Chapter 18: Targeting Touch Devices . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Introducing Gestures and Touch Events ..................................................365 Listening for Gestures .................................................................................368 Looking at an Example Program ................................................................370 viii JavaFX For Dummies Part V: The Part of Tens ............................................ 375 Chapter 19: Ten More JavaFX Controls . . . . . . . . . . . . . . . . . . . . . . . . . 377 TitledPane .....................................................................................................377 Accordion .....................................................................................................379 ColorPicker ...................................................................................................380 DatePicker ....................................................................................................382 Hyperlink ......................................................................................................383 ProgressIndicator and ProgressBar ..........................................................383 Slider .............................................................................................................384 ScrollBar .......................................................................................................386 PasswordField ..............................................................................................387 Chapter 20: Ten Steps to Building a 3D World . . . . . . . . . . . . . . . . . . . 389 Step One: Add a Perspective Camera .......................................................390 Step Two: Add a Cylinder ...........................................................................392 Step Three: Create a Material ....................................................................392 Step Four: Translate the Cylinder .............................................................393 Step Five: Add a Box ....................................................................................394 Step Six: Rotate the Box ..............................................................................395 Step Seven: Add a Sphere ...........................................................................397 Step Eight: Add a Mesh Object ..................................................................398 Step Nine: Animate the Objects .................................................................401 Step Ten: Add a Light Source .....................................................................401 Putting It All Together: The Complete 3D World Program .....................402 Index ....................................................................... 407

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.