JavaFX For Dummies - Doug Lowe - E-Book

JavaFX For Dummies E-Book

Doug Lowe

0,0
20,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Unleash the power of JavaFX for a wide range of devices JavaFX For Dummies gives you access to an innovative software platform that allows you to create and deliver rich Internet applications that can run across a wide variety of connected devices. This accessible book highlights the most important features of this powerful graphics platform, giving you the tools to understand it quickly and easily! No experience with JavaFX? No problem. JavaFX For Dummies has been written especially for newbies and it also serves as a great reference resource for more experienced Java developers. Author Doug Lowe has been writing programming books for decades, and he brings his experience and passion to this guide, sharing his expert approach to coding using JavaFX. The book shows you how to work with JavaFX controls, how to enhance your scenic design, and also offers advice on how to make a splash with your programs. Then, the author wraps it all up with extra recommendations and resources to guide you as you move forward. * Helps developers quickly learn to take advantage of JavaFX's lightweight, high-performance platform * Highlights essential JavaFX features for simple coding that can be rolled out across multiple devices * Instructs readers on methods for creating compelling, visually appealing applications * Includes recommendations and resources for honing your JavaFX skills With JavaFX For Dummies, you'll be on your way to easier, more efficient coding for a variety of connected devices.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 453

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



JavaFX® 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 permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission 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 WITHOUT 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 technical 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

JavaFX® For Dummies®

Visit www.dummies.com/cheatsheet/javafx to view this book's cheat sheet.

Table of Contents

Introduction

About This Book

Foolish Assumptions

How This Book Is Organized

Part I: Getting Started with JavaFX

Part II: JavaFX Controls

Part III: Enhancing Your Scenic Design

Part IV: Making Your Programs Come Alive

Part V: The Part of Tens

Icons Used in This Book

Beyond the Book

Where to Go from Here

Part I: Getting Started with JavaFX

Chapter 1: Hello, JavaFX!

What Is JavaFX?

Perusing the Possibilities of JavaFX

Looking at a Simple JavaFX Program

Downloading and Installing JavaFX

Downloading JDK 8

Installing JDK 8

Setting the path

Developing the Click Me Program with Notepad

Developing the Click Me Program with TextPad

Using an IDE to Create the Click Me Program

Chapter 2: Looking Closer at JavaFX Programming

Looking Again at the Click Me Program

Importing JavaFX Packages

Extending the Application Class

Launching the Application

Overriding the start Method

Creating a Button

Handling an Action Event

Creating a Layout Pane

Making a Scene

Setting the Stage

Examining the Click Counter Program

Chapter 3: Handling Events

Examining Events

Handling Events

Implementing the EventHandler Interface

Handling Events with Inner Classes

Handling Events with Anonymous Inner Classes

Using Lambda Expressions to Handle Events

Chapter 4: Setting the Stage and Scene Layout

Examining the Stage Class

Examining the Scene Class

Switching Scenes

Creating a Dialog Box

Creating a Confirmation Box

Exit, Stage Right

Creating a Close button

Handling the CloseRequest event

Putting it all together

Chapter 5: Using Layout Panes to Arrange Your Scenes

Working with Layout Panes

Introducing four JavaFX layout panes

Creating layout panes

Combining layout panes

Using the HBox Layout

Spacing Things Out

Adding Space with Margins

Adding Space by Growing Nodes

Using the VBox Layout

Aligning Nodes in a Layout Pane

Making Nodes the Same Width

Using the Flow Layout

Using the Border Layout

Chapter 6: Getting Input from the User

Using Text Fields

Validating Numeric Data

Using Check Boxes

Using Radio Buttons

Looking at a Pizza Order Application

Part II: JavaFX Controls

Chapter 7: Introducing the JavaFX Node Hierarchy

An Overview of JavaFX Packages

The Node Class

The Parent Class

The Region Class

The Control Class

Chapter 8: Choosing from a List

Using Choice Boxes

Creating a choice box

Setting a default value

Getting the selected item

Working with Observable Lists

Listening for Selection Changes

Using Combo Boxes

Creating combo boxes

Getting the selected item

Handling combo box events

Using List Views

Creating a list view

Getting the selected items

Using Tree Views

Building a tree

Creating a TreeView control

Getting the selected node

Looking at a complete program that uses a tree view

Chapter 9: Working with Tables

Creating the Data for a Table

Creating a Read-Only Table

Using the TableColumn class

Using the TableView class

A Program That Creates a Read-Only Table

Creating an Editable Table

Adding table rows

Deleting table rows

Editing table cells

A Program That Creates an Editable Table

Chapter 10: Making Menus

Introducing Classes for Creating Menus

Creating a Basic Menu Bar

Creating Menus

Creating Menu Items

Using Separators

Using Action Listeners

Creating Menus That Change

Using Check and Radio Menu Items

Creating Submenus

Creating Custom Menu Items

Part III: Enhancing Your Scenic Design

Chapter 11: More about Layout Panes for Precise Scene Design

Using the StackPane Layout

Using the TilePane layout

Using the ScrollPane Layout

Using the GridPane Layout

Sketching out a plan

Creating a grid pane

Working with grid pane constraints

Examining a grid pane example

Chapter 12: Skinning Your Application with CSS

Using Default Style Sheets

Adding a Style Sheet to a Scene

Using Inline Styling

Creating a Style Sheet

Using type selectors

Creating your own style class names

Using id selectors

Using multiple selectors

Specifying Style Properties

Specifying font properties

Specifying background colors

Specifying border properties

Chapter 13: Drawing Shapes

Introducing the Shape Class

Creating lines

Creating rectangles

Creating circles and ellipses

Creating arcs

Looking at the ShapeMaker program

Fancy Fills

Drawing transparently

Using a gradient fill

Translating, Scaling, and Rotating

Drawing Text

Combining Shapes

Chapter 14: Adding Special Effects

Introducing Special Effects

Adding Shadows

Creating Reflections

Making Things Blurry

Blooming and Glowing

Gaining Perspective

Combining Effects

Part IV: Making Your Programs Come Alive

Chapter 15: Using Properties to Create Dynamic Scenes

Introducing JavaFX Properties

Java API Properties

JavaFX Property Classes

Creating a Read/Write Property

Creating a Read-Only Property

Creating Properties More Efficiently

Using Property Events

Binding Properties

Chapter 16: Using Images and Media

Using Images

Using the Image class

Using the ImageView class

Viewing an Image example

Playing Audio Files

Playing Video Files

Chapter 17: Animating Your Scenes

Introducing JavaFX Animation

Using Transition Classes

Looking at a Transition Example

Combining Transitions

Animating the Hard Way

Improving the Ball Bouncer

Chapter 18: Targeting Touch Devices

Introducing Gestures and Touch Events

Listening for Gestures

Looking at an Example Program

Part V: The Part of Tens

Chapter 19: Ten More JavaFX Controls

TitledPane

Accordion

ColorPicker

DatePicker

Hyperlink

ProgressIndicator and ProgressBar

Slider

ScrollBar

PasswordField

Chapter 20: Ten Steps to Building a 3D World

Step One: Add a Perspective Camera

Step Two: Add a Cylinder

Step Three: Create a Material

Step Four: Translate the Cylinder

Step Five: Add a Box

Step Six: Rotate the Box

Step Seven: Add a Sphere

Step Eight: Add a Mesh Object

Step Nine: Animate the Objects

Step Ten: Add a Light Source

Putting It All Together: The Complete 3D World Program

About the Author

Cheat Sheet

More Dummies Products

Guide

Table of Contents

Begin Reading

Pages

1

2

3

4

5

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

421

422

424

Introduction

In the beginning there was AWT, the Abstract Window Toolkit. AWT was Java’s first system for displaying window-based user interfaces in Java. AWT begat Swing, which soon became the preferred way to create user-friendly applications in Java.

But then there was JavaFX, the worthy successor to the GUI throne. JavaFX is designed to create stunning user interfaces that can run on a wide variety of devices, including traditional desktop and portable computers, tablets, smartphones, TV set-top boxes, game consoles, and many other types of devices.

Until recently, JavaFX was the red-headed stepchild of the Java world. It co-existed with Java, but wasn’t an official part of Java. But beginning with Java version 8, JavaFX is now fully integrated into Java. And while JavaFX and Swing coexist today, Oracle has made it clear that Swing is in its twilight and JavaFX represents the future of user-interface programming.

So you’re holding the right book in your hands. JavaFX is an essential skill for every Java programmer to have at his or her disposal, and this book will help you master that skill.

About This Book

This isn’t the kind of book you pick up and read from start to finish, as if it was a cheap novel. If I ever see you reading it at the beach, I’ll kick sand in your face. Beaches are for reading romance novels or murder mysteries, not programming books.

Assuming, then, that you have found a more suitable location to read this book, you can, if you want, read it straight through starting with Chapter 1 and finishing with Chapter 20. However, this sequence isn’t necessary. If you are brand new to JavaFX programming, I suggest you read at least Part I in sequence so that you’ll gain a basic understanding of how JavaFX works. But after you have the basics down, you can read the chapters in whatever sequence makes sense for you. If you need to know about adding effects to a shape, skip straight to Chapter 14. For information about about animation, skip ahead to Chapter 17.

You don’t have to memorize anything in this book. It’s a need-to-know book: You pick it up when you need to know something. Need a reminder on how to rotate a shape? Pick up the book. Can’t remember the details of the TilePane class? Pick up the book. After you find what you need, put down the book and get on with your life.

This book works like a reference. Start with the topic you want to find out about. Look for it in the Table of Contents or in the index. The Table of Contents is detailed enough that you can find most of the topics you’re looking for. If not, turn to the index, where you can find even more detail.

Of course, the book is loaded with information — so if you want to take a brief excursion into your topic, you’re more than welcome. If you want to know the big picture on the scene graph, read Chapter 7. But if you just want a reminder on how to set the maximum scene size, read just the section on the Scene class.

Whenever I describe sample Java code, I present it as follows:

  @override public void start(Stage primaryStage)

And Java class names, keywords, or other language elements are always shown in monospace type.

Foolish Assumptions

In this book, I make very few assumptions about what you already may or may not know about JavaFX. But I do have to make two basic assumptions:

You own or have access to a computer on which Java JDK 8 has been installed or on which you have permission to install.

JavaFX 8 is an integral part of JDK 8, so JDK 8 is a requirement for figuring out JavaFX. If you have not yet installed it, you’ll find instructions on how to do so in Chapter 1.

You know the basics of Java programming.

If you’re new to Java, may I suggest one of two books: my own Java All-In-One For Dummies, 4th Edition, or Barry Burd’s Java For Dummies, 6th Edition. Both are published by Wiley.

There are no other prerequisites to this book.

How This Book Is Organized

This book is organized into five parts. Here’s a brief description of what you find in each part.

Part I: Getting Started with JavaFX

This part contains the information you need to get started with JavaFX programming. After a brief introduction to what JavaFX is and why it’s so popular, you discover the basics of creating simple JavaFX programs. You figure out how to create simple JavaFX scenes populated with common controls such as labels, text field, and buttons. Then, you find out how to write programs that respond to user input, such as when the user clicks a button or enters text into a text field. And finally, you read how to use basic layout managers to control the arrangement of controls in your JavaFX scene.

Part II: JavaFX Controls

The chapters in this part focus on the various types of controls you can use in a JavaFX application. Chapter 7 starts by explaining the details of how the JavaFX scene graph works and presents the details of the class hierarchy used by the various controls. Then, the remaining chapters in this part present information about specific types of controls, ranging from check boxes and radio buttons to tables and menus.

Part III: Enhancing Your Scenic Design

The chapters in this part help you improve the appearance of your applications. First, you read about additional types of layout managers that give you more precise control over the way your user interface is arranged. Then, you discover how to use CSS styles to apply formatting details. Next, you figure out how to incorporate simple shapes into your scenes. And finally, you can read about JavaFX’s special effects, which let you embellish your display with shadows, motion blurs, and so on.

Part IV: Making Your Programs Come Alive

The chapters in this part focus on various ways to make your programs more responsive and engaging. You discover how to work with properties, which you can use to make one part of your user interface respond to changes in another part of your user interface. Then, you discover how to incorporate media including sound and video. Next, you figure out how to create sophisticated animations that make the objects on the screen dance about. And finally, you read how to create programs that respond to multi-finger gestures on touch-enabled devices.

Part V: The Part of Tens

This wouldn’t be a For Dummies book without a Part of Tens. Each of the chapters here presents ten items of special interest. Chapter 19 presents ten additional JavaFX controls that didn’t fit in Part II. And Chapter 20 presents ten steps to creating a JavaFX application that displays a three-dimensional scene.

Icons Used in This Book

Like any For Dummies book, this book is chock-full of helpful icons that draw your attention to items of particular importance. You find the following icons throughout this book:

Danger, Will Robinson! This icon highlights information that may help you avert disaster.

Did I tell you about the memory course I took?

Pay special attention to this icon; it lets you know that some particularly useful tidbit is at hand.

Hold it — overly technical stuff is just around the corner. Obviously, because this is a programming book, almost every paragraph of the next 400 or so pages could get this icon. So I reserve it for those paragraphs that go into greater depth, down into explaining how something works under the covers — probably deeper than you really need to know to use a feature, but often enlightening. You also sometimes find this icon when I want to illustrate a point with an example that uses some Java feature that hasn’t been covered so far in the book, but that is covered later. In those cases, the icon is just a reminder that you shouldn’t get bogged down in the details of the illustration, and instead focus on the larger point.

Beyond the Book

A lot of extra content that you won’t find in this book is available at www.dummies.com. Go online to find the following:

Online articles covering additional topics at

  www.dummies.com/extras/javafx

Here you find articles covering additional features of JavaFX that didn’t quite fit in the book.

The Cheat Sheet for this book is at

  www.dummies.com/cheatsheet/javafx

Here you’ll find a convenient summary of some of the most important JavaFX classes.

Code listings for this book at

  www.dummies.com/extras/javafx

All the code listings used in this book are available for download.

Updates to this book, if I have any, are also available at

  www.dummies.com/extras/javafx

Where to Go from Here

Yes, you can get there from here. With this book in hand, you’re ready to dive right into to the cool and refreshing water of the JavaFX pool. Browse through the Table of Contents and decide where you want to start. Be bold! Be courageous! Be adventurous! And above all, have fun!

Part I

Getting Started with JavaFX

Visit www.dummies.com for great Dummies content online.

In this part …

Figuring out a basic programHandling eventsDisplaying simple scenesArranging nodesResponding to input controlsVisit www.dummies.com for great Dummies content online.

Chapter 2

Looking Closer at JavaFX Programming

In This Chapter

Importing the classes you need to create a JavaFX program

Creating a class that extends the JavaFX Application class

Using classes such as Button, BorderPane, and Scene to create a user interface

Creating an event handler that will be called when the user clicks a button

Examining an enhanced version of the Click Me program

In Chapter 1, I introduce you to a simple JavaFX program called the Click Me program and briefly describe how that program works. In this chapter, I put this program under the microscope and examine it in close detail. By the time you finish this chapter, you’ll understand how every line of the Click Me program works and why it’s required. Then, you’ll be ready to start figuring out more nuanced techniques of JavaFX programming.

Looking Again at the Click Me Program

Figure 2-1 shows the Click Me program in action. As you can see, this program displays a simple button that contains the words Click me please!. What the figure does not show is that when the user clicks the button, the text on the button becomes I’ve Been Clicked!.

Figure 2-1:The Click Me program in action.

Although this program is simple, it demonstrates most of the essential techniques you need to master to figure out how to write JavaFX programs:

It displays a user interface that includes a standard type of user interface control — in this case, a button.It responds to the user’s input, generated when the user clicks the button.It updates the display to confirm the user’s action.