Chapter 14 JavaFX Basics

2y ago
22 Views
5 Downloads
2.87 MB
43 Pages
Last View : 12d ago
Last Download : 1m ago
Upload by : Jacoby Zeller
Transcription

Chapter 14 JavaFX BasicsLiang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.1

Using JavaFX in EclipseDownload an appropriate JavaFX runtime (https://gluonhq.com/products/javafx/ )for your operating system and unzipit to a desired location. Put the contents of this zip file somewhere on your system, for example �.METHOD I:Install the JavaFx plug-in for eclipse.- With Eclipse open, click Help Install New Software - Click the Add button.- Enter a name, such as JavaFX.- Enter the location as follows: ased/1.2.0/site/- Check both options “e(fx)clipse – install” and “e(fx)clipse – single components” as shown. Accept other defaults.Click the Next button.- These are the items that will be installed. Click Next again.- Accept terms and click Finish.- You’ll be prompted to reboot Eclipse. The plug-in is now installed and is ready to use.File - Properties - Java Build Path - Libraries tab - Add external jarsselect and include all files in the directory that has unzipped files (for example �)Now, to use JavaFx.- Navigate to File New Project- In the New Project wizard, select JavaFx JavaFX Project- Enter a name for the project, ie, JavaFxDemo, etc. Click Finish.- Go to the project name src application. Click once on application to highlight. Create classes here (right-mouseclick, New Class)Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.2

Using JavaFX in EclipseMETHOD II:- File - Properties - Java Build Path - Libraries tab - Add external jars- select and include all files in the directory that has unzipped files (for .2\lib”)- Windows - Preferences - Java - Installed JREs - Edit- Put--module-path "C:\Users\selim\Documents\javafx-sdk-11.0.2\lib" aphics,javafx.media- in Default VM Arguments. Note that your JavaFX library location can be different depending on thelocation that you unzipped.METHOD III:From CMD Prompt:java --module-path "C:\Users\selim\Documents\javafx-sdk-11.0.2\lib" --add-modules javafx.controlsBounceBallControlLiang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.3

Motivations JavaFX is a new framework for developing JavaGUI programs. The JavaFX API is an excellentexample of how the object-oriented principle isapplied.This chapter serves two purposes. First, it presentsthe basics of JavaFX programming. Second, it usesJavaFX to demonstrate OOP. Specifically, thischapter introduces the framework of JavaFX anddiscusses JavaFX GUI components and theirrelationships.Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.4

Objectives To distinguish between JavaFX, Swing, and AWT (§14.2).To write a simple JavaFX program and understand the relationship among stages,scenes, and nodes (§14.3).To create user interfaces using panes, UI controls, and shapes (§14.4).To use binding properties to synchronize property values (§14.5).To use the common properties style and rotate for nodes (§14.6).To create colors using the Color class (§14.7).To create fonts using the Font class (§14.8).To create images using the Image class and to create image views using theImageView class (§14.9).To layout nodes using Pane, StackPane, FlowPane, GridPane, BorderPane, HBox,and VBox (§14.10).To display text using the Text class and create shapes using Line, Circle, Rectangle,Ellipse, Arc, Polygon, and Polyline (§14.11).To develop the reusable GUI components ClockPane for displaying an analog clock(§14.12).Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.5

JavaFX vs Swing and AWT Swing and AWT are replaced by the JavaFX platform fordeveloping rich Internet applications. When Java was introduced, the GUI classes were bundled in alibrary known as the Abstract Windows Toolkit (AWT).AWT is fine for developing simple graphical user interfaces,but not for developing comprehensive GUI projects. Inaddition, AWT is prone to platform-specific bugs.The AWT user-interface components were replaced by a morerobust, versatile, and flexible library known as Swingcomponents. Swing components are painted directly oncanvases using Java code.Swing components depend less on the target platform and useless of the native GUI resource. With the release of Java 8,Swing is replaced by a completely new GUI platform knownas JavaFX. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.6

Basic Structure of JavaFX Application Override the start(Stage) method Stage, Scene, and Nodes A JavaFX GUI Program extends fromjavafx.application.Application (justlike a Java Swing GUI programextends from javax.swing.JFrame). JavaFX names the Stage and Sceneclasses using the analogy from thetheater. You may think of stage as theplatform to support scenes, and nodesas actors to perform in the scenes.StageSceneButtonMyJavaFXLiang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.Run7

Basic Structure of JavaFX The launch method (line 22) is a static methoddefined in the Application class for launching astand-alone JavaFX application.The main method (lines 21–23) is not needed ifyou run the program from the command line. Itmay be needed to launch a JavaFX program froman IDE with a limited JavaFX support. When yourun a JavaFX application without a main method,JVM automatically invokes the launch method torun the application.The main class overrides the start method definedin javafx.application.Application (line 8).After a JavaFX application is launched, the JVMconstructs an instance of the class using its no-argconstructor and invokes its start method.The start method normally places UI controls in ascene and displays the scene in a stage.Line 10 creates a Button object and places it in aScene object (line 11). A Scene object can becreated using the constructor Scene(node,width, height). This constructor specifies thewidth and height of the scene and places the nodein the scene.Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.8

Basic Structure of JavaFX A Stage object is a window. A Stage object called primarystage is automatically created by the JVM when the applicationis launched.Line 13 sets the scene to the primary stage and line 14 displaysthe primary stage.JavaFX names the Stage and Scene classes using the analogyfrom the theater.You may think of stage as the platform to support scenes, andnodes as actors to perform in the scenes.You can create additional stages if needed. The JavaFX programin Listing 14.2 displays two stages, as shown in Figure 14.2b.MultipleStageDemoRunLiang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.9

MultipleStageDemoLiang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.All rights reserved.10

How It Works1.JavaFX uses the metaphor of a theater to model the graphics application. A stage (defined bythe javafx.stage.Stage class) represents the top-level container (window). The individual controls (orcomponents) are contained in a scene (defined by the javafx.scene.Scene class). An application canhave more than one scenes, but only one of the scenes can be displayed on the stage at any given time.The contents of a scene is represented in a hierarchical scene graph of nodes (definedby ja

.media - in Default VM Arguments. Note that your JavaFX library location can be different depending on the location that you unzipped. METHOD III: From CMD Prompt: java --module-path "C:\Users\selim\Documents\javafx-sdk-11.0.2\lib" --add-m

Related Documents:

This chapter describes how to add JavaFX co ntent into a Swing application and how to use threads correctly when both Swing and JavaFX content operate within a single application. JavaFX SDK provides the JFXPanel class, which is located in the javafx.embed.swing package and enables you to embed

This tutorial is a compilation of three documents that were previously delivered with the JavaFX 2.x documentation set: JavaFX Overview, JavaFX Architecture, and Getting Started with JavaFX.

Oct 10, 2011 · Support for ARM platforms has also been made available with JavaFX 8. JDK for ARM includes the base, graphics and controls components of JavaFX. To install JavaFX install your chosen version of the Java Runtime environment and Java Development kit. Features offered by JavaFX include

What You Will Learn 1 1.1 Background Basics 2 JavaFX Integration 2 1.2 The NetBeans Platform: The Big Picture 3 Module System API 3 . 2.4 Improving the User Experience 63 2.5 Concurrency and Thread Safety 68 . 3.2 Building JavaFX Programs 87 Creating a JavaFX Application 88 Java APIs 88

Database Operations in JavaFX . At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an Oracle XE database. In this post, we will create controller, model, DAO, and Util classes to do DB operations. Before starting to code, I want to give more information about our example .

Swing includes a . JFXPanel. class to embed JavaFX components into a Swing application, and JavaFX includes a . SwingNode. class to embed Swing components into a JavaFX application. Both classes provide corresponding API documentation. Additionally, the official Java documentation site includes an arti

General rules 8 Tecniche di programmazione A.A. 2020/2021 A JavaFX application extends javafx.application.Application The main() method should call Application.launch() The start() method is the main entry point for all JavaFX applications Called with a Stage connected to the Operating System's window The content of the scene is represented as a hierarchical

Have a brain storming session where they generate 10-15 themes. (Be patient, they'll get there eventually.) After they generate the list, allow people to talk in behalf of specific ideas Sometimes they may combine items Give everyone 3 votes and go through the list voting. If there is a clear winner then proceed. Otherwise repeat the process, but with one vote. Post the chosen theme .